﻿
using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.x_Novatek
{
    public partial class Hi3288 : Form
    {

        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        private double[,] D_lums;//目标值-单色
        private double[] D_lum = new double[25];//目标值-白色
        private byte[] Send_buf = new byte[1];
        List<string> lines;
        private byte[] Rec_tmp = new byte[1];
        private byte[] Rec_tmp1 = new byte[1];
        private List<byte> buffer = new List<byte>(4096);//默认分配1/16页内存，并始终限制不允许超过  
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        //监听NT37701A挂载
        string[] DBV_SET = { "", "" };
        string[] GAMMA_SET = { "", "" };
        private List<string> ImageType;

        public static bool firstGammaFlag = true;   //初次打开子窗体标志位
        frmMain frmTop = null;
        public Hi3288(frmMain DOE_Hi3288)
        {
            frmTop = DOE_Hi3288;
            InitializeComponent();
            frmTop.Child_window_hide_already = true;
            frmTop.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体  

            string[] s = { ".bmp", ".dsc" };
            this.ImageType = new List<string>(s);
        }

        #region   //=================================LCD cfg function:OLED_Show_Click====Start=================
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void chkBISTMode_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkBISTMode.Checked)
            {   //退出BIST 模式
                byte[] TepArr;
                TepArr = new byte[] {
                        13,0xFF,0xFE,0x29,0xFF,0x07,0x00,0x00,0x39,0x38,0x00,0x0,0x0,0x00
                       };
                try
                {
                    frmTop.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾                  
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
            }
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            byte[] TepArr;
            byte rr = 0, gg = 0, bb = 0;
            byte brr, bgg, bbb;
            int rr10, bb10, gg10;
            Int32 rgbtep;
            byte tep = ((byte)numPatternR.Value);
            Button btnTmp = (Button)sender;
            if ((!chkBISTMode.Checked) & (!chkGammaLoadBMP.Checked))
            {
                Send_buf = new byte[25];
                Setting.TempRecord.cmdLen = 25;
                Send_buf[0] = 0x0A; //LCD配置标志
                Send_buf[2] = 0x15; //数据长度
                switch (btnTmp.Name)
                {
                    case "btnPatternW":                             //写入Pattern参数
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                            Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                            Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                        }
                        break;
                    case "btnred":
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = 0xFF;
                            Send_buf[4] = 0;
                            Send_buf[5] = 0;
                        }
                        break; //R
                    case "btngreen":
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = 0;
                            Send_buf[4] = 0xFF;
                            Send_buf[5] = 0;
                        }
                        break; //G
                    case "btnblue":
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = 0;
                            Send_buf[4] = 0;
                            Send_buf[5] = 0xFF;
                        }
                        break; //B
                    case "btnwhite":
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = 0xFF;
                            Send_buf[4] = 0xFF;
                            Send_buf[5] = 0xFF;
                        }
                        break; //W
                    case "btnblack":
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = 0;
                            Send_buf[4] = 0;
                            Send_buf[5] = 0;
                        }
                        break; //BL
                    default:
                        break;
                }

                CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
                try
                {
                    frmTop.comPort.Write(Send_buf, 0, 25);
                }
                catch
                {
                    MessageBox.Show("OLED_Show串口有误，请检查", "错误");
                }
            }
            else if ((chkGammaLoadBMP.Checked))
            {
                btnPrevPic_Click(null, null);
            }
            else    //aod & bistPttern
            {
                if (chkAODMode.Checked)
                {
                    // BIST 模式
                    switch (btnTmp.Name)
                    {
                        case "btnPatternW":          //灰阶
                            {
                                rr = (byte)numPatternR.Value;
                                gg = (byte)numPatternG.Value;
                                bb = (byte)numPatternB.Value;
                            }
                            break;
                        case "btnred":
                            {
                                rr = 255;
                                gg = 0;
                                bb = 0;
                            }
                            break; //R
                        case "btngreen":
                            {
                                rr = 0;
                                gg = 255;
                                bb = 0;
                            }
                            break; //G
                        case "btnblue":
                            {
                                rr = 0;
                                gg = 0;
                                bb = 255;
                            }
                            break; //B
                        case "btnwhite":
                            {
                                rr = 255;
                                gg = 255;
                                bb = 255;
                            }
                            break; //W
                        case "btnblack":
                            {
                                rr = 0;
                                gg = 0;
                                bb = 0;
                            }
                            break; //BL
                        default:
                            break;
                    }
                    rr10 = rr * 4 + 3; gg10 = gg * 4 + 3; bb10 = bb * 4 + 3;
                    //rr10 = rr * 4; gg10 = gg * 4; bb10 = bb * 4;
                    rgbtep = (bb10 + (gg10 << 10) + (rr10 << 20));
                    rgbtep &= 0x3fffffff;
                    TepArr = new byte[] {
                        13,0xFF,0xFE,0x29,0xFF,07,00,00,0x39,0x38,0x5B,0x9F,0x25,00,
                        13,0xFF,0xFE,0x29,0xFF,07,00,0x04,0x39,0x38,00,00,00,00,
                        13,0xFF,0xFE,0x29,0xFF,07,00,0x08,0x39,0x38,0xDF,0x34,0xA7,0x00,
                        13,0xFF,0xFE,0x29,0xFF,07,00,0x38,0x39,0x38,(byte)((rgbtep>>0)&0xff),(byte)((rgbtep>>8)&0xff),(byte)((rgbtep>>16)&0xff),(byte)((rgbtep>>24)&0xff),
                        13,0xFF,0xFE,0x29,0xFF,05,00,0x34,0x39,0x00,0x00,0x01,0x00,0x00,
                        13,0xFF,0xFE,0x29,0xFF,05,00,0x38,0x39,0x00,0xb0,0x04,0x08,0x07,
                    };
                    //F_WRITE_REGS_FOR_HI(0X07003800, 0X00259F5B)
                    //F_WRITE_REGS_FOR_HI(0X07003804, 0X00000000)
                    //F_WRITE_REGS_FOR_HI(0X07003808, 0X00A734DF)
                    //F_WRITE_REGS_FOR_HI(0X07003838, PatternData)
                    //F_WRITE_REGS_FOR_HI(0X05000034, 0X100)
                    //F_WRITE_REGS_FOR_HI(0X05000038, 0X70804B0)

                }
                else if (chkBISTPattern.Checked)
                {  //bist 模式下的功能图，待修复
                    //byte rr, gg, bb;
                    //byte tep = ((byte)numPatternR.Value);
                    rr = tep;
                    gg = tep;
                    bb = tep;
                    TepArr = new byte[] {
                        1,0x28,
                        1,0x10,
                        6,0xf0,0x55,0xaa,0x52,8,0,
                        6,0xef,0x00,0x1f,rr,gg,bb,
                        2,0xee,1,
                       };
                }
                else
                {
                    // BIST 模式下的灰阶
                    //byte[] TepArr;
                    //byte Color = 1;
                    //byte GrayLevel = 1;
                    switch (btnTmp.Name)
                    {
                        case "btnPatternW":          //灰阶
                            {
                                rr = (byte)numPatternR.Value;
                                gg = (byte)numPatternG.Value;
                                bb = (byte)numPatternB.Value;
                            }
                            break;
                        case "btnred":
                            {
                                rr = 255;
                                gg = 0;
                                bb = 0;
                            }
                            break; //R
                        case "btngreen":
                            {
                                rr = 0;
                                gg = 255;
                                bb = 0;
                            }
                            break; //G
                        case "btnblue":
                            {
                                rr = 0;
                                gg = 0;
                                bb = 255;
                            }
                            break; //B
                        case "btnwhite":
                            {
                                rr = 255;
                                gg = 255;
                                bb = 255;
                            }
                            break; //W
                        case "btnblack":
                            {
                                rr = 0;
                                gg = 0;
                                bb = 0;
                            }
                            break; //BL
                        default:
                            break;
                    }
                    rr10 = rr * 4 + 3; gg10 = gg * 4 + 3; bb10 = bb * 4 + 3;
                    //rr10 = rr * 4; gg10 = gg * 4; bb10 = bb * 4;
                    rgbtep = (bb10 + (gg10 << 10) + (rr10 << 20));
                    rgbtep &= 0x3fffffff;
                    TepArr = new byte[] {
                      13,0xFF,0xFE,0x29,0xFF,07,00,00,0x39,0x38,0x5B,0x9F,0x25,00,
                        //13,0xFF,0xFE,0x29,0xFF,07,00,0x04,0x39,0x38,00,00,00,00,
                        //13,0xFF,0xFE,0x29,0xFF,07,00,0x08,0x39,0x38,0xDF,0x34,0xA7,0x00,
                        13,0xFF,0xFE,0x29,0xFF,07,00,0x38,0x39,0x38,(byte)((rgbtep>>0)&0xff),(byte)((rgbtep>>8)&0xff),(byte)((rgbtep>>16)&0xff),(byte)((rgbtep>>24)&0xff)
                       };
                }  //aod & bistPttern
                try
                {
                    frmTop.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾                  
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
            } //aod & bistPttern
            if (chkLvShow.Checked)
            {
                Msr_CA310();
                Thread.Sleep(50);
                auto_lum = frmTop.CASDK_LUM;
                auto_x = frmTop.CASDK_x;
                auto_y = frmTop.CASDK_y;
                txttip.Text = "Lv: " + auto_lum + "\r\n";
            }

        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox_Gray.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void checkBox_Gray_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Gray.Checked)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternR.Value;
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }

        #endregion //=================================LCD cfg function: OLED_Show_Click====end=================     
        #region  //---Hi3288专用界面  打开与否的相关处理  以及 串口 返回数据 处理- --- serial Data----
        private void Hi3288_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            ////frmTop.comPort.Close()
            frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(Hi3288_DataReceived);
            frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
            ////frmTop.comPort.Open();
            txttip.Text = txttip.Text + "Hi3288_Gamma_FormClosed！\r\n";
            frmTop.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmTop.Child_window_hide_already = false;
            frmTop.IC_comboBox.Enabled = true;
            firstGammaFlag = true;
        }
        private void Hi3288_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmTop.Child_window_hide_already == false) //子窗体已开启标识
            {
                ////frmTop.comPort.Close()
                frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(Hi3288_DataReceived);
                frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
                ////frmTop.comPort.Open();
                txttip.Text = txttip.Text + "Hi3288_FormHide！\r\n";
                frmTop.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
                frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(Hi3288_DataReceived);
                txttip.Text = txttip.Text + "Hi3288_FormOpened！\r\n";
                frmTop.Child_window_hide_already = false; //子窗体已开启标识
            }

        }
        private void Hi3288_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing)
            { return; }
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmTop.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;
                Setting.TempRecord.cmdLen = n;
                frmTop.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    //RECEIVE
                    // 0x0A: 模组Timing参数 设置  返回
                    // 0x0D: 模组的SSD2828配置数据、DriverIC初始化数据  设置  返回
                    // 0x2F: Demura操作    设置  返回
                    // 0x99: 通用读写、寄存器读写  设置  返回
                    // 0x9E: 功耗测试单元  设置  返回
                    if ((buffer[0] == 0x99) && (Setting.TempRecord.cmdLen > 0))
                    {

                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }
                    }
                    else if ((buffer[0] == 0x6B) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);
                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }

                    }
                    else if (((buffer[0] == 0x1D) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x2F) || (buffer[0] == 0x99) || (buffer[0] == 0x9e) || (buffer[0] == 0x9f)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }
                    }

                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmTop.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x6B) //
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        //  if (Rec_tmp[4] == 0x00)
                                        //     frmTop.txttip.Text = frmTop.txttip.Text + "DDIC 寄存器\t写 OK！\r\n";
                                        txttip.Text = String.Format("{0:X2}", Rec_tmp[3]) + "寄存器写OK\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        string tmp = "";                    //8bit  read
                                        //reText.Text = "";
                                        {
                                            for (int i = 0; i <= (Rec_tmp[4] + 1); i++)
                                            {
                                                if (i == (Rec_tmp[4] + 1))
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i + 3]);//16进制

                                                }
                                                else if (i == 1)
                                                {
                                                    //不显示回读位数
                                                }
                                                else
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i + 3]) + ",";//16进制
                                                }

                                            }
                                        }
                                        // frmTop.Generate_Read_Result_txt.AppendText(tmp + "\r\n");
                                        // reText.Text = tmp;
                                        Application.DoEvents();
                                    }
                                    break;
                                case 0x09:                 //read gamma
                                    {
                                        // if (Rec_tmp[4] == 0x00)
                                        txttip.Text = txttip.Text + "Gamma寄存器Read OK！\r\n";
                                        // frmTop.txttip.Text = frmTop.txttip.Text + BitConverter.ToString(Rec_tmp);
                                        int i = 5;
                                        //Array.Copy(Rec_tmp, 5, GammaValueReceived, 0, 150);
                                        for (i = 0; i < 25; i++)
                                        {
                                            GammaValue[i, 0] = (uint)(Rec_tmp[i * 2 + 5] << 8) + Rec_tmp[i * 2 + 6];
                                            GammaValue[i, 1] = (uint)(Rec_tmp[i * 2 + 5 + 50] << 8) + Rec_tmp[i * 2 + 6 + 50];
                                            GammaValue[i, 2] = (uint)(Rec_tmp[i * 2 + 5 + 100] << 8) + Rec_tmp[i * 2 + 6 + 100];
                                        }

                                        for (i = 0; i < 25; i++)
                                        {
                                            DataGirdShow((byte)i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                                        }
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        //  if (Rec_tmp[4] == 0x00)
                                        //     frmTop.txttip.Text = frmTop.txttip.Text + "DDIC 寄存器\t写 OK！\r\n";
                                        txttip.Text = "gamma OTP OK\r\n";
                                    }
                                    break;
                                default: break;

                            }
                        }
                        //--------------------------------------------Power Test---------------
                        else if (Rec_tmp[0] == 0x9E)  // PowerTest
                        {
                            switch (Rec_tmp[3])
                            {
                                case 0x00:
                                    {
                                        double PMU_ddic_power = 0;
                                        double PMU_panel_power = 0;
                                        double PMU_power = 0;
                                        //VCI = (float)(BitConverter.ToUInt16(Rec_tmp, 12) * 0.00125); if (VCI < 0.05) { VCI = 0; }
                                        //VCIC = (float)(BitConverter.ToUInt16(Rec_tmp, 14) * 2.5) / (float)(262);//250
                                        //VDDIO = (float)(BitConverter.ToUInt16(Rec_tmp, 16) * 0.00125); if (VDDIO < 0.05) { VDDIO = 0; }
                                        // VDDIOC = (float)(BitConverter.ToUInt16(Rec_tmp, 18) * 2.5) / (float)(137);//120
                                        //AVDD = (float)(BitConverter.ToUInt16(Rec_tmp, 8) * 0.00125); if (AVDD < 0.05) { AVDD = 0; }
                                        //AVDDC = (float)(BitConverter.ToUInt16(Rec_tmp, 10) * 2.5) / (float)(120);
                                        //ELVDD = (float)(BitConverter.ToUInt16(Rec_tmp, 4) * 0.00125); if (ELVDD < 0.05) { ELVDD = 0; }
                                        ELVDDC = (float)(BitConverter.ToUInt16(Rec_tmp, 6) * 2.5) / 120;
                                        ELVDDC = ELVDDC - (float)(0.6112 - 0.007674 * ELVDDC);
                                        txttip.Text += ELVDDC + "mA \r\n";
                                        //ELVSS = (float)(BitConverter.ToUInt16(Rec_tmp, 28) * 0.00157); if (ELVSS < 0.05) { ELVSS = 0; }
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        //--------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        txttip.Text += "Demura RAM Enable\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        txttip.Text += "Demura RAM Addr2 Enable\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        txttip.Text += "Demura RAM Addr3 Addr Enable\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        txttip.Text += "Demura RAM Addr4 Addr Enable\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        txttip.Text += "Demura RAM Disable\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0xff)
                                        {
                                            txttip.Text += "Demura RAM write Flash fail ! \r\n";
                                            // MessageBox.Show("载入失败！");
                                        }
                                        else
                                        {
                                            txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash Complete ! \r\n";
                                            // MessageBox.Show("载入完成！");
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash2 Complete ! \r\n";
                                        // MessageBox.Show("载入完成！");
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash3 Complete ! \r\n";
                                        // MessageBox.Show("载入完成！");
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash4 Complete ! \r\n";
                                        //  MessageBox.Show("载入完成！");
                                    }
                                    break;

                                case 0x04:
                                    {
                                        txttip.Text += "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "," + Rec_tmp[7].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x05:
                                    {
                                        txttip.Text += "Flash Erase Success ! \r\n";
                                    }
                                    break;
                                case 0x06:
                                    {
                                        txttip.Text += "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x07:
                                    {
                                        txttip.Text += "Demura function on \r\n";
                                    }
                                    break;
                                case 0x08:
                                    {
                                        txttip.Text += "Demura function off \r\n";
                                    }
                                    break;
                                case 0x09:
                                    {
                                        txttip.Text += "Demura OTP complete \r\n";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        //--------------------------------------------通用读写----------------------------------------------
                        else if (Rec_tmp[0] == 0x99) //GENWR
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            txttip.Text = txttip.Text + "DDIC 寄存器\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        string tmp = "";                    //8bit  read
                                        {
                                            for (int i = 3; i <= (Rec_tmp[2] + 2); i++)
                                            {
                                                readValue <<= 8;
                                                if (i == (Rec_tmp[2] + 2))
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);//16进制
                                                }
                                                else
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                                }
                                                // ReadBackarr[i] = Rec_tmp[i];
                                                readValue += Rec_tmp[i];
                                                readValue = (readValue & 0xffffffff);
                                            }
                                            readValue = ((readValue & 0xff) << 24) + (((readValue & 0xff00) >> 8) << 16) + (((readValue & 0xff0000) >> 16) << 8) + (((readValue & 0xff000000) >> 24) << 0);

                                            GammaReadBackFlag = false;
                                        }
                                        txttip.Text += "DDIC 8bit寄存器--读 OK！\r\n";
                                        txttip.AppendText(tiptxtFlag + tmp + "\r\n");
                                        Application.DoEvents();
                                        tiptxtFlag = "";
                                        readokflag = true;
                                    }
                                    break;
                                case 0x03:
                                    {
                                        string tmp = "";
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[3]) + ",";  //16进制
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[4]) + ",";//16进制
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[5]);      //16进制

                                        txttip.Text += "DDIC 16bit寄存器--读 OK！\r\n";
                                        txttip.AppendText(tmp + "\r\n");
                                        Application.DoEvents();
                                        readokflag = true;

                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                        {
                                            txttip.Text += "vpp off！\r\n";
                                        }
                                        else if (Rec_tmp[3] == 0x01)
                                        {
                                            txttip.Text += "vpp on！\r\n";
                                        }
                                    }
                                    break;
                            }
                        }
                        //----------------------------------------------------------------------------------------------------                       
                        else if (Rec_tmp[0] == 0x9f)    //小黄窗的通用命令读写
                        {
                            showmsg("[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "命令已执行" + "\r\n", 1);
                        }
                    }));
                }
            }
            finally
            {
                Listening = false;
                //readokflag = true;
            }
        }
        private byte[] GetCRCArray(byte[] arr, Byte speedflag)   //处理命令数组，在其头尾增加包头以及CRC校验值，处理后的数据可以直接发送至下位机
        {
            CRC16 CRCData = new CRC16();
            int arrNumbuer;                                                                                                                                                                                                                                                                                                                                             //51
            arrNumbuer = arr.Length;
            arrNumbuer += 6;
            if (arrNumbuer > 511)
            {
                showmsg("***发送数据超长，请检查***");
            }
            byte[] Send_Datax = new byte[arrNumbuer];
            Send_Datax[0] = 0x9f;
            Send_Datax[1] = (byte)((speedflag << 4) + (((arr.Length + 1) >> 8) & 0x01));
            Send_Datax[2] = Convert.ToByte((arr.Length + 1) & 0xFF);  //为兼容下位机命令，此位填上 待传输的总数据长度+1 

            Send_Datax[arr.Length + 3] = 0xFF;  //添加结束标志位
            arr.CopyTo(Send_Datax, 3);//合并数组头 4个Byte 标志位
            //计算校验码                                                 
            CRCData.CalculateCrc16(Send_Datax, out Send_Datax[arrNumbuer - 2], out Send_Datax[arrNumbuer - 1]);
            return Send_Datax;
        }
        #endregion //---串口返回数据处理- --- serial Data----
        private void GR_WriteRead_Click(object sender, EventArgs e)          //上位机使用通用命令读写方式操作DDIC
        {
            string str = regSend.Text.ToString();
            byte DDICReg; byte readnum;
            string[] arr;
            arr = str.Split(',');
            Setting.TempRecord.cmdLen = arr.Length + 5;
            //Send_buf = new byte[Setting.TempRecord.cmdLen];
            //Send_buf[2] = Convert.ToByte(arr.Length);
            Button btnTmp = (Button)sender;
            //Send_buf[0] = 0x6B;

            switch (btnTmp.Name)
            {
                case "writeReg":
                    {
                        String text;
                        text = regSend.Text;
                        frmTop.txtProcessDrivercode(text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        if (rBt_LP_DCS_Long_Write.Checked | rBt_LP_DCS_Short_Write.Checked | rBt_LP_Gerneric_Long_Write.Checked | rBt_LP_Gerneric_Short_Write.Checked)
                        {
                            frmTop.SendCmd(0);  //低速
                        }
                        else
                        {
                            frmTop.SendCmd(4); //高速
                        }
                        txttip.Text += "命令已下发...\r\n";
                        txttip.Select(txttip.Text.Length, 0);
                        txttip.ScrollToCaret();
                    }
                    break;
                case "readReg":
                    {
                        //Hi3288_0XFE_XX = 0x00;
                        //Send_buf[1] = 0x02;
                        DDICReg = byte.Parse(Convert.ToUInt32(arr[0], 16).ToString());
                        try
                        {
                            readnum = byte.Parse(Convert.ToUInt32(arr[1], 16).ToString());
                            frmTop.GeneralRegRead(DDICReg, readnum);
                        }
                        catch
                        {
                            txttip.Text += "读参数异常....\r\n";
                        }

                    }
                    break;

                default:
                    break;
            }
            //for (int i = 0; i < arr.Length; i++)
            //{
            //    Send_buf[3 + i] = byte.Parse(Convert.ToUInt32(arr[i], 16).ToString());
            //}
            //CRCData.CalculateCrc16(Send_buf, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            //try
            //{
            //    frmTop.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);
            //}
            //catch
            //{
            //    MessageBox.Show("串口有误，请检查", "错误");
            //}
            frmTop.T1.Enabled = false;
            frmTop.T1.Interval = 2000;
            frmTop.T1.Enabled = true;
        }           //上位机使用通用命令读写方式操作DDIC     
        private void btnRun_Click(object sender, EventArgs e)   //---复位屏幕，重新点屏
        {
            frmTop.btnRun_Click(frmTop.btnRun, e);
        }
        #region    //---Auto Gamma----------------------
        byte gammaindex = 0;
        private void btnSaveGamma_Click(object sender, EventArgs e)
        {
            string tepGammaR = ""; string tepGammaG = ""; string tepGammaB = "";
            string tepstr = "";
            string Hi3288Gammatxt = "";
            //Int16[] GammaPoint = new Int16[25] { 1, 2, 3, 5, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 71, 79, 95, 111, 127, 159, 191, 223, 255 };
            int index = 0;// txt51Dbv.GetFirstCharIndexOfCurrentLine();
            int line = 0;// txt51Dbv.GetLineFromCharIndex(index);
            Int16 gdattmp;
            {
                Button btnTmp = (Button)sender;
                switch (btnTmp.Name)
                {
                    case "btnSaveGamma_Auto":
                        {
                            //以文本形式保存
                            if (chkGammaFormatForHi3288.Checked && chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString(), 10);
                                    tepGammaR += Convert.ToString(gdattmp) + "\r\n";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString(), 10);
                                    tepGammaG += Convert.ToString(gdattmp) + "\r\n";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString(), 10);
                                    tepGammaB += Convert.ToString(gdattmp) + "\r\n";
                                }
                                string str = "";
                                for (int i = 0; i < 25; i++)
                                {
                                    str += Point_Hi3288[i] + "\r\n";
                                }
                                SaveFileDialog txt = new SaveFileDialog();
                                txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                                //string GammaStr = "//-----保存Gamma 寄存器值-----\r\n";
                                if (txt.ShowDialog() == DialogResult.OK)
                                {
                                    if (txt.FileName == " ")
                                    {
                                        return;
                                    }
                                    StreamWriter gsw = null; //文件流
                                    gsw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                                    try
                                    {
                                        tepGammaB = tepGammaB.Substring(0, tepGammaB.Length - 2);
                                        gsw.Write(str + tepGammaR + tepGammaG + tepGammaB);
                                        gsw.Dispose(); //释放资源
                                        gsw.Close(); //关闭数据流
                                        MessageBox.Show("保存Gamma 寄存器保存OK！", "系统提示：");
                                    }
                                    catch
                                    {
                                        MessageBox.Show("保存Gamma 寄存器保存时发生错误，请再试！", "系统提示：");
                                    }
                                }
                            }
                            else if (chkGammaFormatForHi3288.Checked && !chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式                                    
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }
                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";

                                string str = "";
                                //if (rbt_Hi3288_Gamma9.Checked) { str = "// ---Gamma 9 九--  -\r\n //Gamma set 9--  -\r\n bf,08\r\n"; }
                                //else if (rbt_Hi3288_Gamma10.Checked) { str = "// ---Gamma 10 十---\r\n //Gamma set 10---\r\n bf,09\r\n"; }
                                //else if (rbt_Hi3288_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                //else if (rbt_Hi3288_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                //else if (rbt_Hi3288_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                //else if (rbt_Hi3288_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                //else if (rbt_Hi3288_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                //else if (rbt_Hi3288_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                //else if (rbt_Hi3288_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                //else if (rbt_Hi3288_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                //------------------------------------------------------------------------
                                SaveFileDialog txt = new SaveFileDialog();
                                txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                                //string GammaStr = "//-----保存Gamma 寄存器值-----\r\n";
                                if (txt.ShowDialog() == DialogResult.OK)
                                {
                                    if (txt.FileName == " ")
                                    {
                                        return;
                                    }
                                    StreamWriter gsw = null; //文件流
                                    gsw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                                    try
                                    {
                                        //richTextBox_GC_In.Text += str + Hi3288Gammatxt;
                                        gsw.Write(str + Hi3288Gammatxt);
                                        gsw.Dispose(); //释放资源
                                        gsw.Close(); //关闭数据流
                                        MessageBox.Show("保存Gamma 寄存器保存OK！", "系统提示：");
                                    }
                                    catch
                                    {
                                        MessageBox.Show("保存Gamma 寄存器保存时发生错误，请再试！", "系统提示：");
                                    }
                                }
                            }
                            else if (!chkGammaFormatForHi3288.Checked && chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }

                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";
                                //------------------------------------------------------------------------
                                string str = "";
                                //if (rbt_Hi3288_Gamma9.Checked) { str = "// ---Gamma 9 九-- -\r\n //Gamma set 9-  --\r\n bf,08\r\n"; }
                                //else if (rbt_Hi3288_Gamma10.Checked) { str = "// ---Gamma 10 十---\r\n //Gamma set 10---\r\n bf,09\r\n"; }
                                //else if (rbt_Hi3288_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                //else if (rbt_Hi3288_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                //else if (rbt_Hi3288_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                //else if (rbt_Hi3288_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                //else if (rbt_Hi3288_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                //else if (rbt_Hi3288_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                //else if (rbt_Hi3288_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                //else if (rbt_Hi3288_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                //txtGammaTest.Text += (str + Hi3288Gammatxt);
                                //richTextBox_GC_In.Text += (str + Hi3288Gammatxt);
                            }
                            else
                            {//保存

                                //SaveFileDialog sf = new SaveFileDialog(); //对话框
                                //sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
                                string str = "灰阶序号,灰阶亮度,reg_R,reg_G,reg_B\r\n";
                                //if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                                {
                                    //if (sf.FileName == "") return; //如果没有输入文件名
                                    try
                                    {
                                        StreamWriter sw = null; //文件流
                                        string fname = Application.StartupPath + @"\l_gamma\HI3288" + gammaindex + "_" + txtPanelID.Text + ".csv";
                                        filePath = Application.StartupPath + @"\l_gamma";
                                        sw = new StreamWriter(fname, false, Encoding.UTF8);
                                        for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                        {
                                            str = str + this.GammaReg_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString() + "\r\n";
                                        }
                                        sw.WriteLine(str);
                                        sw.Dispose(); //释放资源
                                        sw.Close(); //关闭数据流
                                        showmsg("数据保存OK！");

                                    }
                                    catch
                                    {
                                        showmsg("数据保存时发生错误，请再试！");
                                    }
                                }

                            }
                        }
                        break;
                    case "btnSaveGamma":
                        {
                            //以文本形式保存
                            if (chkGammaFormatForHi3288.Checked && chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString(), 10);
                                    tepGammaR += Convert.ToString(gdattmp) + "\r\n";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString(), 10);
                                    tepGammaG += Convert.ToString(gdattmp) + "\r\n";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString(), 10);
                                    tepGammaB += Convert.ToString(gdattmp) + "\r\n";
                                }
                                string str = "";
                                for (int i = 0; i < 25; i++)
                                {
                                    str += Point_Hi3288[i] + "\r\n";
                                }
                                SaveFileDialog txt = new SaveFileDialog();
                                txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                                //string GammaStr = "//-----保存Gamma 寄存器值-----\r\n";
                                if (txt.ShowDialog() == DialogResult.OK)
                                {
                                    if (txt.FileName == " ")
                                    {
                                        return;
                                    }
                                    StreamWriter gsw = null; //文件流
                                    gsw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                                    try
                                    {
                                        tepGammaB = tepGammaB.Substring(0, tepGammaB.Length - 2);
                                        gsw.Write(str + tepGammaR + tepGammaG + tepGammaB);
                                        gsw.Dispose(); //释放资源
                                        gsw.Close(); //关闭数据流
                                        MessageBox.Show("保存Gamma 寄存器保存OK！", "系统提示：");
                                    }
                                    catch
                                    {
                                        MessageBox.Show("保存Gamma 寄存器保存时发生错误，请再试！", "系统提示：");
                                    }
                                }
                            }
                            else if (chkGammaFormatForHi3288.Checked && !chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式                                    
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }
                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";

                                string str = "";
                                //if (rbt_Hi3288_Gamma9.Checked) { str = "// ---Gamma 9 九--  -\r\n //Gamma set 9--  -\r\n bf,08\r\n"; }
                                //else if (rbt_Hi3288_Gamma10.Checked) { str = "// ---Gamma 10 十---\r\n //Gamma set 10---\r\n bf,09\r\n"; }
                                //else if (rbt_Hi3288_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                //else if (rbt_Hi3288_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                //else if (rbt_Hi3288_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                //else if (rbt_Hi3288_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                //else if (rbt_Hi3288_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                //else if (rbt_Hi3288_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                //else if (rbt_Hi3288_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                //else if (rbt_Hi3288_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                //------------------------------------------------------------------------
                                SaveFileDialog txt = new SaveFileDialog();
                                txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                                //string GammaStr = "//-----保存Gamma 寄存器值-----\r\n";
                                if (txt.ShowDialog() == DialogResult.OK)
                                {
                                    if (txt.FileName == " ")
                                    {
                                        return;
                                    }
                                    StreamWriter gsw = null; //文件流
                                    gsw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                                    try
                                    {
                                        //richTextBox_GC_In.Text += str + Hi3288Gammatxt;
                                        gsw.Write(str + Hi3288Gammatxt);
                                        gsw.Dispose(); //释放资源
                                        gsw.Close(); //关闭数据流
                                        MessageBox.Show("保存Gamma 寄存器保存OK！", "系统提示：");
                                    }
                                    catch
                                    {
                                        MessageBox.Show("保存Gamma 寄存器保存时发生错误，请再试！", "系统提示：");
                                    }
                                }
                            }
                            else if (!chkGammaFormatForHi3288.Checked && chkHi3288dircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }

                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                Hi3288Gammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";
                                //------------------------------------------------------------------------
                                string str = "";
                                //if (rbt_Hi3288_Gamma9.Checked) { str = "// ---Gamma 9 九-- -\r\n //Gamma set 9-  --\r\n bf,08\r\n"; }
                                //else if (rbt_Hi3288_Gamma10.Checked) { str = "// ---Gamma 10 十---\r\n //Gamma set 10---\r\n bf,09\r\n"; }
                                //else if (rbt_Hi3288_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                //else if (rbt_Hi3288_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                //else if (rbt_Hi3288_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                //else if (rbt_Hi3288_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                //else if (rbt_Hi3288_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                //else if (rbt_Hi3288_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                //else if (rbt_Hi3288_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                //else if (rbt_Hi3288_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                //txtGammaTest.Text += (str + Hi3288Gammatxt);
                                //richTextBox_GC_In.Text += (str + Hi3288Gammatxt);
                            }
                            else
                            {//保存

                                SaveFileDialog sf = new SaveFileDialog(); //对话框
                                sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
                                string str = "灰阶序号,灰阶亮度,reg_R,reg_G,reg_B\r\n";
                                if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                                {
                                    if (sf.FileName == "") return; //如果没有输入文件名
                                    try
                                    {
                                        StreamWriter sw = null; //文件流                                       
                                        sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                        for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                        {
                                            str = str + this.GammaReg_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString() + "\r\n";
                                        }
                                        sw.WriteLine(str);
                                        sw.Dispose(); //释放资源
                                        sw.Close(); //关闭数据流
                                        showmsg("数据保存OK！");

                                    }
                                    catch
                                    {
                                        showmsg("数据保存时发生错误，请再试！");
                                    }
                                }

                            }
                        }
                        break;
                    case "btnLoadGamma_Auto":
                        {
                            this.GammaReg_dataGridView.Rows.Clear();
                            //OpenFileDialog sf = new OpenFileDialog();
                            int Line_Count = 0;
                            string strtmp = "";
                            //sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                            //sf.Title = "数据导入";
                            //if (sf.ShowDialog() == DialogResult.OK) //如果确定
                            {
                                string fname = Application.StartupPath + @"\l_gamma\HI3288" + gammaindex + "_" + txtPanelID.Text + ".csv";
                                try
                                {
                                    FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
                                    StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                                    streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                                    string strLine = streamReader.ReadLine();
                                    while (!string.IsNullOrEmpty(strLine))
                                    {
                                        strtmp = strtmp + strLine + ",";
                                        strLine = streamReader.ReadLine(); //循环读取下一行
                                        Line_Count++;
                                    }
                                    fs.Close(); //关闭流
                                    streamReader.Close();
                                }
                                catch { return; }
                                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                                for (int j = 1; j <= Line_Count - 1; j++)
                                {
                                    DataGridViewRow Row1 = new DataGridViewRow();
                                    GammaReg_dataGridView.Rows.Add(Row1);
                                }
                                try
                                {
                                    for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                    {
                                        GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[5 * (i + 1) + 0];
                                        GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[5 * (i + 1) + 1];
                                        GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[5 * (i + 1) + 2];
                                        GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[5 * (i + 1) + 3];
                                        GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[5 * (i + 1) + 4];
                                        //------------------------------------
                                        //index = txt51Dbv.GetFirstCharIndexOfCurrentLine();
                                        //line = txt51Dbv.GetLineFromCharIndex(index); //获取当前鼠标所在行
                                        line = gammaindex;//此处line设定为第line条Gamma
                                        {
                                            GammaValx[line, i, 0] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 2]);
                                            GammaValx[line, i, 1] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 3]);
                                            GammaValx[line, i, 2] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 4]);
                                        }
                                    }
                                    showmsg("数据导入OK！");
                                }
                                catch
                                {
                                    MessageBox.Show("数据导入时发生错误！", "提示：");
                                }
                            }
                        }
                        break;
                    case "btnLoadGamma":
                        {
                            string strLine;
                            this.GammaReg_dataGridView.Rows.Clear();
                            OpenFileDialog sf = new OpenFileDialog();
                            int Line_Count = 0;
                            string strtmp = "";
                            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                            sf.Title = "数据导入";
                            if (sf.ShowDialog() == DialogResult.OK) //如果确定
                            {
                                StreamReader streamReader; FileStream fs;
                                try
                                {
                                    fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                                    streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                                    streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                                    strLine = streamReader.ReadLine();
                                }
                                catch
                                {
                                    MessageBox.Show("读取异常，检查CSV是否被锁定");
                                    return;
                                }
                                while (!string.IsNullOrEmpty(strLine))
                                {
                                    strtmp = strtmp + strLine + ",";
                                    strLine = streamReader.ReadLine(); //循环读取下一行
                                    Line_Count++;
                                }
                                fs.Close(); //关闭流
                                streamReader.Close();
                                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                                for (int j = 1; j <= Line_Count - 1; j++)
                                {
                                    DataGridViewRow Row1 = new DataGridViewRow();
                                    GammaReg_dataGridView.Rows.Add(Row1);
                                }
                                try
                                {
                                    for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                    {
                                        GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[5 * (i + 1) + 0];
                                        GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[5 * (i + 1) + 1];
                                        GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[5 * (i + 1) + 2];
                                        GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[5 * (i + 1) + 3];
                                        GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[5 * (i + 1) + 4];
                                        //------------------------------------
                                        //index = txt51Dbv.GetFirstCharIndexOfCurrentLine();
                                        //line = txt51Dbv.GetLineFromCharIndex(index); //获取当前鼠标所在行
                                        line = gammaindex;//此处line设定为第line条Gamma
                                        {
                                            GammaValx[line, i, 0] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 2]);
                                            GammaValx[line, i, 1] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 3]);
                                            GammaValx[line, i, 2] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 4]);
                                        }
                                    }
                                    showmsg("数据导入OK！");
                                }
                                catch
                                {
                                    MessageBox.Show("数据导入时发生错误！", "提示：");
                                }
                            }
                        }
                        break;
                    case "btn_CAS200_Clean":
                        {
                            DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                            if (dr == DialogResult.OK)
                            {
                                this.GammaReg_dataGridView.Rows.Clear();
                            }
                        }
                        break;

                    default: break;
                }
            }
        }
        private byte Gammnum = 8;

        private double D_x, D_y;
        //private int index = 0;
        private double lum_off, x_off, y_off;
        private double R_x = 0.685;//
        private double R_y = 0.315;
        private double G_x = 0.255;
        private double G_y = 0.7055;
        private double B_x = 0.135;
        private double B_y = 0.055;
        private uint[,] GammaValue = new uint[36, 3];//保存当前Gammavalue
        private double auto_lum;  //  存放获取到的产品亮度值
        private double auto_x;
        private double auto_y;        //  存放获取到的产品亮度 色坐标 x,y
        private int index = 1;  //AutoGamma 调整顺序
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        bool lastdirupR = true; bool lastdirupG = true; bool lastdirupB = true; // Gamma 调整方向
        private uint step_l = 1;   //Gamma 调节步长
        byte rstep = 16; byte gstep = 16; byte bstep = 16;
        bool STOPAutoGammaFlag = false;     //AutoGamma 停止标志位，解决定时器禁止后AutoGamma无法停止的问题
        UInt32[,] GammaValueTeparr = new UInt32[18 * 3 + 2, 2]; //存放格式化后的GammaValue，以便于直接下发至DDIC
        byte[] ReadBackarr = new byte[32];  //暂存下位机返回数据
        bool GammaReadBackFlag = false; // 下位机返回数据完成标志位
        byte[] GammaValueReceived = new byte[50 * 3]; //存放从下位机接收过来的GammaValue        
        bool ReceivOKFlag = false;
        private int[] Point_Hi3288 = new int[1] {//---Gamma绑点数据，这里的数据仅供参考---
            255};

        //byte GPN =  (byte) Point_Hi3288.Length; // 获取Gamma绑点数量
        byte GPN = 36; //保存Gamma绑点数量
        private void btnCalGammaValue_Click(object sender, EventArgs e)   //计算对应绑点灰阶值，并连同暂存的Gammavale显示在窗体表格上
        {
            //txtGammaTest.Clear();
            txtGammaNode_TextChanged(null, null);  //获取Gamma绑点数据   //GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            byte tep = (byte)Point_Hi3288.Length;
            D_lum = new double[tep];
            this.GammaReg_dataGridView.Rows.Clear();
            for (byte i = 0; i < tep; i++)
            {
                D_lum[i] = (Math.Pow((Point_Hi3288[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
                DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                //txtGammaTest.Text += "Gamma Node" + i + ": " + Point_Hi3288[i] + "\r\n";
            }
        }
        UInt32[,] GammaVxxx = new UInt32[18 * 3 + 2, 2];

        uint[] GammabinArrOrg_32 = new uint[] {
            //[BCMG_LUT]
            0X0B326FA0, 0X00000000,
            0X0B326FA4, 0X00000000,
            0X0B326FA8, 0X00B00000,
            0X0B326FAC, 0X00020000,
            0X0B326FB0, 0X08000040,
            0X0B326FB4, 0X00B00B00,
            0X0B326FB8, 0X00010000,
            0X0B326FBC, 0X04000020,
            0X0B326FC0, 0X00401600,
            0X0B326FC4, 0X00008000,
            0X0B326FC8, 0X02000010,
            0X0B326FCC, 0X00A01A00,
            0X0B326FD0, 0X00008000,
            0X0B326FD4, 0X02000010,
            0X0B326FD8, 0X00C02400,
            0X0B326FDC, 0X00008000,
            0X0B326FE0, 0X02000010,
            0X0B326FE4, 0X00C03000,
            0X0B326FE8, 0X00008000,
            0X0B326FEC, 0X02000010,
            0X0B326FF0, 0X00C03C00,
            0X0B326FF4, 0X00008000,
            0X0B326FF8, 0X02000010,
            0X0B326FFC, 0X00804800,
            0X0B327000, 0X00004000,
            0X0B327004, 0X01000008,
            0X0B327008, 0X00000000,
            0X0B32700C, 0X00004000,
            0X0B327010, 0X01000008,
            0X0B327014, 0X00B00000,
            0X0B327018, 0X00004000,
            0X0B32701C, 0X01000008,
            0X0B327020, 0X01600000,
            0X0B327024, 0X00004000,
            0X0B327028, 0X01000008,
            0X0B32702C, 0X01A00000,
            0X0B327030, 0X00004000,
            0X0B327034, 0X01000008,
            0X0B327038, 0X02400000,
            0X0B32703C, 0X00002000,
            0X0B327040, 0X00800004,
            0X0B327044, 0X03000000,
            0X0B327048, 0X00001000,
            0X0B32704C, 0X00400002,
            0X0B327050, 0X03C00000,
            0X0B327054, 0X00001000,
            0X0B327058, 0X00400002,
            0X0B32705C, 0X04800000,
            0X0B327060, 0X00000800,
            0X0B327064, 0X00200001,
            0X0B327068, 0X00000000,
            0X0B32706C, 0X00001000,
            0X0B327070, 0X00400002,
            0X0B327074, 0X00000000,
            0X0B327078, 0X00002000,
            0X0B32707C, 0X00800004,
            0X0B327080, 0X00000000,
            0X0B327084, 0X00000000,
            0X0B327088, 0X00000000,
            0X0B32708C, 0X00000000,
            0X0B327090, 0X00000000,
            0X0B327094, 0X00000000,
            0X0B327098, 0X00000000,
            0X0B32709C, 0X00000000,
            0X0B3270A0, 0X00000000,
            0X0B3270A4, 0X00000000,
            0X0B3270A8, 0X00000000,
            0X0B3270AC, 0X00000000,
            0X0B3270B0, 0X00000000,
            0X0B3270B4, 0X00000000,
            0X0B3270B8, 0X00000000,
            0X0B3270BC, 0X00000000,
            0X0B3270C0, 0X00000000,
            0X0B3270C4, 0X00000000,
            0X0B3270C8, 0X00000000,
            0X0B3270CC, 0X00000000,
            0X0B3270D0, 0X00000000,
            0X0B3270D4, 0X00000000,
            0X0B3270D8, 0X00000000,
            0X0B3270DC, 0X00000000,
            0X0B3270E0, 0X00000000,
            0X0B3270E4, 0X00000000,
            0X0B3270E8, 0X00000000,
            0X0B3270EC, 0X00000000,
            0X0B3270F0, 0X00000000,
            0X0B3270F4, 0X00000000,
            0X0B3270F8, 0X00000000,
            0X0B3270FC, 0X00000000,
            0X0B327100, 0X00000000,
            0X0B327104, 0X00000000,
            0X0B327108, 0X00000000,
            0X0B32710C, 0X00000000,
            0X0B327110, 0X00000000,
            0X0B327114, 0X00000000,
            0X0B327118, 0X00000000,
            0X0B32711C, 0X00000000,
            0X0B327120, 0X00000000,
            0X0B327124, 0X00000000,
            0X0B327128, 0X00000000,
            0X0B32712C, 0X00000000,
            0X0B327130, 0X00000000,
            0X0B327134, 0X00000000,
            0X0B327138, 0X00000000,
            0X0B32713C, 0X00000000,
            0X0B327140, 0X00000000,
            0X0B327144, 0X00000000,
            0X0B327148, 0X00000000,
            0X0B32714C, 0X00000000,
            0X0B327150, 0X00242424,
            0X0B327154, 0X24240000,
            0X0B327158, 0X00000024,
            0X0B32715C, 0X00242424,
            0X0B327160, 0X24240000,
            0X0B327164, 0X00000024,
            0X0B327168, 0X00242424,
            0X0B32716C, 0X24240000,
            0X0B327170, 0X00000024,
            0X0B327174, 0X00242424,
            0X0B327178, 0X24240000,
            0X0B32717C, 0X00000024,
            0X0B327180, 0X00242424,
            0X0B327184, 0X00000000,
            0X0B327188, 0X00000000,
            0X0B32718C, 0X00000000,
            0X0B327190, 0X24240000,
            0X0B327194, 0X00000024,
            0X0B327198, 0X00242424,
            0X0B32719C, 0X24240000,
            0X0B3271A0, 0X00000024,
            0X0B3271A4, 0X00242424,
            0X0B3271A8, 0X24240000,
            0X0B3271AC, 0X00000024,
            0X0B3271B0, 0X00242424,
            0X0B3271B4, 0X24240000,
            0X0B3271B8, 0X00000024,
            0X0B3271BC, 0X00242424,
            0X0B3271C0, 0X24240000,
            0X0B3271C4, 0X00000024,
            0X0B3271C8, 0X00000000,
            0X0B3271CC, 0X00000000,
            0X0B3271D0, 0X00000000,
            0X0B3271D4, 0X00242424,
            0X0B3271D8, 0X24240000,
            0X0B3271DC, 0X00000024,
            0X0B3271E0, 0X00242424,
            0X0B3271E4, 0X24240000,
            0X0B3271E8, 0X00000024,
            0X0B3271EC, 0X00000000,
            0X0B3271F0, 0X00000000,
            0X0B3271F4, 0X00000000,
            0X0B3271F8, 0X00000000,
            0X0B3271FC, 0X00000000,
            0X0B327200, 0X00000000,
            0X0B327204, 0X00000000,
            0X0B327208, 0X00000000,
            0X0B32720C, 0X00000000,
            0X0B327210, 0X00000000,
            0X0B327214, 0X00000000,
            0X0B327218, 0X00000000,
            0X0B32721C, 0X00000000,
            0X0B327220, 0X00000000,
            0X0B327224, 0X00000000,
            0X0B327228, 0X00000000,
            0X0B32722C, 0X00000000,
            0X0B327230, 0X00000000,
            0X0B327234, 0X00000000,
            0X0B327238, 0X00000000,
            0X0B32723C, 0X00000000,
            0X0B327240, 0X00000000,
            0X0B327244, 0X00000000,
            0X0B327248, 0X00000000,
            0X0B32724C, 0X00000000,
            0X0B327250, 0X00000000,
            0X0B327254, 0X00000000,
            0X0B327258, 0X00000000,
            0X0B32725C, 0X00000000,
            0X0B327260, 0X00000000,
            0X0B327264, 0X00000000,
            0X0B327268, 0X00000000,
            0X0B32726C, 0X00000000,
            0X0B327270, 0X00000000,
            0X0B327274, 0X00000000,
            0X0B327278, 0X00000000,
            0X0B32727C, 0X00000000,
            0X0B327280, 0X00000000,
            0X0B327284, 0X00000000,
            0X0B327288, 0X00000000,
            0X0B32728C, 0X00000000,
            0X0B327290, 0X00000000,
            0X0B327294, 0X00000000,
            0X0B327298, 0X00000000,
            0X0B32729C, 0X00000000,
            0X0B3272A0, 0X00000000,
            0X0B3272A4, 0X00000000,
            0X0B3272A8, 0X00000000,
            0X0B3272AC, 0X00000000,
            0X0B3272B0, 0X00000000,
            0X0B3272B4, 0X00000000,
            0X0B3272B8, 0X00000000,
            0X0B3272BC, 0X00000000,
            0X0B3272C0, 0X00000000,
            0X0B3272C4, 0X00000000,
            0X0B3272C8, 0X00000000,
            0X0B3272CC, 0X00000000,
            0X0B3272D0, 0X00000000,
            0X0B3272D4, 0X00000000,
            0X0B3272D8, 0X00000000,
            0X0B3272DC, 0X00000000,
            0X0B3272E0, 0X00000000,
            0X0B3272E4, 0X00000000,
            0X0B3272E8, 0X00000000,
            0X0B3272EC, 0X00000000,
            0X0B3272F0, 0X00000000,
            0X0B3272F4, 0X00000000,
            0X0B3272F8, 0X00000000,
            0X0B3272FC, 0X00000000,
            0X0B327300, 0X00000000,
            0X0B327304, 0X00000000,
            0X0B327308, 0X00000000,
            0X0B32730C, 0X00000000,
            0X0B327310, 0X00000000,
            0X0B327314, 0X00000000,
            0X0B327318, 0X00000000,
            0X0B32731C, 0X00000000,
            0X0B327320, 0X00000000,
            0X0B327324, 0X00000000,
            0X0B327328, 0X00000000,
            0X0B32732C, 0X00000000,
            0X0B327330, 0X00000000,
            0X0B327334, 0XB0000040,
            0X0B327338, 0X00000018,
            0X0B32733C, 0X8B0003BC,
            0X0B327340, 0X90006441,
            0X0B327344, 0X00000032,
            0X0B327348, 0XC2000C14,
            0X0B32734C, 0X100160C6,
            0X0B327350, 0X000000DC,
            0X0B327354, 0XD0001610,
            0X0B327358, 0X00022E0F,
            0X0B32735C, 0X000000FD,
            0X0B327360, 0XD00036EC,
            0X0B327364, 0X0003FC0F,
            0X0B327368, 0X000000FD,
            0X0B32736C, 0XD0003FF8,
            0X0B327370, 0X0000000F,
            0X0B327374, 0X00000000,
            0X0B327378, 0X8B000004,
            0X0B32737C, 0XB0003BC1,
            0X0B327380, 0X00000018,
            0X0B327384, 0X29000644,
            0X0B327388, 0X9000C143,
            0X0B32738C, 0X00000069,
            0X0B327390, 0XC100160C,
            0X0B327394, 0XC001610D,
            0X0B327398, 0X000000FD,
            0X0B32739C, 0XDC0022E0,
            0X0B3273A0, 0XC0036ECF,
            0X0B3273A4, 0X000000FD,
            0X0B3273A8, 0XDC003FC0,
            0X0B3273AC, 0XC003FF8F,
            0X0B3273B0, 0X000000FD,
            0X0B3273B4, 0X00000000,
            0X0B3273B8, 0X00005540,
            0X0B3273BC, 0X000000F4,
            0X0B3273C0, 0X40001000,
            0X0B3273C4, 0X0003FF8F,
            0X0B3273C8, 0X000000F4,
            0X0B3273CC, 0X00000000,
            0X0B3273D0, 0X00000000,
            0X0B3273D4, 0X00000000,
            0X0B3273D8, 0X00000000,
            0X0B3273DC, 0X00000000,
            0X0B3273E0, 0X00000000,
            0X0B3273E4, 0X00000000,
            0X0B3273E8, 0X00000000,
            0X0B3273EC, 0X00000000,
            0X0B3273F0, 0X00000000,
            0X0B3273F4, 0X00000000,
            0X0B3273F8, 0X00000000,
            0X0B3273FC, 0X00000000,
            0X0B327400, 0X00000000,
            0X0B327404, 0X00000000,
            0X0B327408, 0X00000000,
            0X0B32740C, 0X00000000,
            0X0B327410, 0X00000000,
            0X0B327414, 0X00000000,
            0X0B327418, 0X00000000,
            0X0B32741C, 0X00000000,
            0X0B327420, 0X00000000,
            0X0B327424, 0X00000000,
            0X0B327428, 0X00000000,
            0X0B32742C, 0X00000000,
            0X0B327430, 0X00000000,
            0X0B327434, 0X00000000,
            0X0B327438, 0X00000000,
            0X0B32743C, 0X00000000,
            0X0B327440, 0X00000000,
            0X0B327444, 0X00000000,
            0X0B327448, 0X00000000,
            0X0B32744C, 0X00000000,
            0X0B327450, 0X00000000,
            0X0B327454, 0X00000000,
            0X0B327458, 0X00000000,
            0X0B32745C, 0X00000000,
            0X0B327460, 0X00000000,
            0X0B327464, 0X00000000,
            0X0B327468, 0X00000000,
            0X0B32746C, 0X00000000,
            0X0B327470, 0X00000000,
            0X0B327474, 0X00000000,
            0X0B327478, 0X00000000,
            0X0B32747C, 0X00000000,
            0X0B327480, 0X00000000,
            0X0B327484, 0X00000000,
            0X0B327488, 0X00000000,
            0X0B32748C, 0X00000000,
            0X0B327490, 0X00000000,
            0X0B327494, 0X00000000,
            0X0B327498, 0X00000000,
            0X0B32749C, 0X00000000,
            0X0B3274A0, 0X00000000,
            0X0B3274A4, 0X00000000,
            0X0B3274A8, 0X00000000,
            0X0B3274AC, 0X00000000,
            0X0B3274B0, 0X00000000,
            0X0B3274B4, 0X00000000,
            0X0B3274B8, 0X00000000,
            0X0B3274BC, 0X00000000,
            0X0B3274C0, 0X00000000,
            0X0B3274C4, 0X00000000,
            0X0B3274C8, 0X00000000,
            0X0B3274CC, 0X00000000,
            0X0B3274D0, 0X00000000,
            0X0B3274D4, 0X00000000,
            0X0B3274D8, 0X00000000,
            0X0B3274DC, 0X00000000,
            0X0B3274E0, 0X00000000,
            0X0B3274E4, 0X00000000,
            0X0B3274E8, 0X00000000,
            0X0B3274EC, 0X00000000,
            0X0B3274F0, 0X00000000,
            0X0B3274F4, 0X00000000,
            0X0B3274F8, 0X00000000,
            0X0B3274FC, 0X00000000,
            0X0B327500, 0X00000000,
            0X0B327504, 0X00000000,
            0X0B327508, 0X00000000,
            0X0B32750C, 0X00000000,
            0X0B327510, 0X00000000,
            0X0B327514, 0X00100100,
            0X0B327518, 0X00000001,
            0X0B32751C, 0X30030030,
            0X0B327520, 0X00700700,
            0X0B327524, 0X00000007,
            0X0B327528, 0XB00B00B0,
            0X0B32752C, 0X00F00F00,
            0X0B327530, 0X0000000F,
            0X0B327534, 0X30130130,
            0X0B327538, 0X01701701,
            0X0B32753C, 0X00000017,
            0X0B327540, 0XF01F01F0,
            0X0B327544, 0X02702701,
            0X0B327548, 0X00000027,
            0X0B32754C, 0XF02F02F0,
            0X0B327550, 0X03703702,
            0X0B327554, 0X00000037,
            0X0B327558, 0XF03F03F0,
            0X0B32755C, 0X04F04F03,
            0X0B327560, 0X0000004F,
            0X0B327564, 0XF06F06F0,
            0X0B327568, 0X08F08F06,
            0X0B32756C, 0X0000008F,
            0X0B327570, 0XF0CF0CF0,
            0X0B327574, 0X0EF0EF0C,
            0X0B327578, 0X000000EF,
            0X0B32757C, 0XF0FF0FF0,
            0X0B327580, 0X0FF0FF0F,
            0X0B327584, 0X000000FF,
            0X0B327588, 0XF0FF0FF0,
            0X0B32758C, 0X0FF0FF0F,
            0X0B327590, 0X000000FF,
            0X0B327594, 0XF0FF0FF0,
            0X0B327598, 0X0FF0FF0F,
            0X0B32759C, 0X000000FF,
            0X0B3275A0, 0XF0FF0FF0,
            0X0B3275A4, 0X0FF0FF0F,
            0X0B3275A8, 0X000000FF,
            0X0B3275AC, 0XF0FF0FF0,
            0X0B3275B0, 0X0FF0FF0F,
            0X0B3275B4, 0X000000FF,
            0X0B3275B8, 0XF0FF0FF0,
            0X0B3275BC, 0X0FF0FF0F,
            0X0B3275C0, 0X000000FF,
            0X0B3275C4, 0XF0FF0FF0,
            0X0B3275C8, 0X0FF0FF0F,
            0X0B3275CC, 0X000000FF,
            0X0B3275D0, 0XF0FF0FF0,
            0X0B3275D4, 0X0FF0FF0F,
            0X0B3275D8, 0X000000FF,
            0X0B3275DC, 0XF0FF0FF0,
            0X0B3275E0, 0X0FF0FF0F,
            0X0B3275E4, 0X000000FF,
        };
        uint[] GammabinArr = new uint[9408 >> 2];  //实际需要占用字节为2346*4=9384，但为了96字节对齐设置为  96*98=9408
        void GetGammabin()
        {
            for (uint i = 0; i < 402; i++)
            {
                GammabinArr[i] = GammabinArrOrg_32[2 * i + 1];
            }
            uint para1, para2, para3, r, g, b, r1, g1, b1, Gammaband, gbn = 0;
            for (Gammaband = 0; Gammaband < 26; Gammaband++)
            {    // （GammaBand 0 默认设置为0）,所以从Gammaband1开始写入即可         
                if (Gammaband < 11)
                {
                    gbn = 11 - Gammaband - 1;
                }
                if ((chkGammaCOPY.Checked) && (Gammaband > 11) && (Gammaband < 22))
                {  //第11条Gamma填0，不用管
                    gbn = 22 - Gammaband - 1;
                    //GammavalMove(0, (int)Gammaband - 1 - 11, GammaValx, GammaValue);
                }//节约时间，临时填一套Gamma进去

                //AOD Gamma暂不做考虑
                GammavalMove(0, (int)gbn, GammaValx, GammaValue);
                for (int i = (GPN); i < 36; i++)
                {
                    GammaValue[i, 0] = GammaValue[GPN - 1, 0];
                    GammaValue[i, 1] = GammaValue[GPN - 1, 1];
                    GammaValue[i, 2] = GammaValue[GPN - 1, 2];
                }
                //把第n组Gamma从GammaValx搬出至GammaValue
                //int movedir, int n, uint[,,] a_GroupGammaVal, uint[,] b_GammaVal
                //调Gamma时，UI上第一条Gamma放置在GammaValx[0,x,x]位置，对应GammaBand 1（GammaBand 0 默认设置为0）

                for (uint i = 0; i < 18; i++)  //11+11+4个Band
                {
                    r = GammaValue[2 * i, 0];
                    g = GammaValue[2 * i, 1];
                    b = GammaValue[2 * i, 2];

                    r1 = GammaValue[2 * i + 1, 0];
                    g1 = GammaValue[2 * i + 1, 1];
                    b1 = GammaValue[2 * i + 1, 2];

                    para1 = b + (g << 12) + ((r & 0xff) << 24);
                    para2 = (r >> 8) + (b1 << 4) + (g1 << 16) + ((r1 & 0xf) << 28);
                    para3 = (r1 & 0xff0) >> 4;

                    GammabinArr[402 + Gammaband * 54 + 3 * i] = para1;
                    GammabinArr[402 + Gammaband * 54 + 3 * i + 1] = para2;
                    GammabinArr[402 + Gammaband * 54 + 3 * i + 2] = para3;
                }
            }
            //----------------------------------------------------------
            try
            {
                string GammaBinfname = Application.StartupPath + @"\x_Hisi3288\HI3288Gammbin" + "_" + txtPanelID.Text + ".bin";
                filePath = Application.StartupPath + @"\x_Hisi3288";
                byte[] binfileArr = new byte[9408];
                for (int i = 0; i < (2352); i++)   //2352=9408/4
                {
                    binfileArr[4 * i + 0] = (byte)((GammabinArr[i] >> 0) & 0xff);
                    binfileArr[4 * i + 1] = (byte)((GammabinArr[i] >> 8) & 0xff);
                    binfileArr[4 * i + 2] = (byte)((GammabinArr[i] >> 16) & 0xff);
                    binfileArr[4 * i + 3] = (byte)((GammabinArr[i] >> 24) & 0xff);
                }
                //在Gamma.bin第9377Byte处写入"D2"(CGColorID); 第9378Byte写入"80"(CVER客户版本号);
                //第9379 - 9399Byte处写入21Byte 由扫码枪获取的SN信息；
                binfileArr[9376] = 0xD2;
                binfileArr[9377] = 0x80;
                string sn = "abcdef1234567890aabbc";
                for (int m = 0; m < 21; m++)
                {
                    byte[] arrt = System.Text.Encoding.ASCII.GetBytes(sn);
                    binfileArr[9378 + m] = (byte)((arrt[m]));
                }
                Console.WriteLine(sn);
                binfileArr[binfileArr.Length - 1] = 0x7a;
                binfileArr[binfileArr.Length - 2] = 0x66;
                binfileArr[binfileArr.Length - 3] = 0x75;
                binfileArr[binfileArr.Length - 4] = 0x68;//测试位
                //--------------------------------------------
                System.IO.FileStream fs = File.OpenWrite(GammaBinfname);   //  //输出文件                        
                fs.Write(binfileArr, 0, binfileArr.Length);
                fs.Close(); fs.Dispose();
                showmsg("数据保存OK！");
            }
            catch
            {
                showmsg("数据保存时发生错误，请再试！");
            }

        }
        private void GammaValueFormat_Click(object sender, EventArgs e)   //GammaValue 值拆分成 Byte 并与实际地址进行转换 以便于后续处理发送至下位机
        {
            uint addr, para1, para2, para3, addr_S, r, g, b, r1, g1, b1;
            //txtGammaTest.Clear();
            uint cfgoffset, GammaupdateAddr;
            GammaupdateAddr = 0x00042588;  //!!!每次海思变更初始化时一定注意此3个(cfgoffset,GammaupdateAddr,BCMG_LUT_Start_Addr;)地址是否更新
            uint BCMG_LUT_Start_Addr = 0X0031C260;  //0X0031C260; 20220805
            //0x0031D460;  //-- Gram Address of BCMG-LUT(0x0B31D460).Defined by Hisilicon. 
            cfgoffset = 0X0B31C8A8 - BCMG_LUT_Start_Addr;
            if (rdoModelC11.Checked)
            {
                BCMG_LUT_Start_Addr = 0x0b000000 + 0x326FA0;   //BCMG_LUT_Start_Addr   //0X0B326FA0
                GammaupdateAddr = (BCMG_LUT_Start_Addr - 0x0b000000) / 12;  // 0X00043070;                
                cfgoffset = 402 * 4;   //0X0B3275E8 =0X0B326FA0+402*4
            }
            addr_S = (uint)(Gammnum) * 54 * 4 + BCMG_LUT_Start_Addr + cfgoffset;  // 每组Gamma多用54个地址来填0            
                                                                                  //36个绑点，每个绑点RGB 3个12bit值，每两个绑点值放入3个地址，设定每条Gamma需要54个地址；

            for (uint i = 0; i < 54; i++)
            {
                addr = addr_S + i * 4;
                GammaValueTeparr[i, 0] = addr;
            }
            for (uint i = 0; i < 18; i++)
            {
                addr = addr_S + i * 4;
                r = GammaValue[2 * i, 0];
                g = GammaValue[2 * i, 1];
                b = GammaValue[2 * i, 2];

                r1 = GammaValue[2 * i + 1, 0];
                g1 = GammaValue[2 * i + 1, 1];
                b1 = GammaValue[2 * i + 1, 2];

                para1 = b + (g << 12) + ((r & 0xff) << 24);
                para2 = (r >> 8) + (b1 << 4) + (g1 << 16) + ((r1 & 0xf) << 28);
                para3 = (r1 & 0xff0) >> 4;

                GammaValueTeparr[3 * i, 1] = para1;
                GammaValueTeparr[3 * i + 1, 1] = para2;
                GammaValueTeparr[3 * i + 2, 1] = para3;
            }
            string str = "";
            for (uint i = 0; i < 54; i++)
            {
                str += "0x" + GammaValueTeparr[i, 0].ToString("X8") + ",0x" + GammaValueTeparr[i, 1].ToString("X8") + "\r\n";
            }
            GammaValueTeparr[54, 0] = 0x0600309C;   //更新Gamma数据
            GammaValueTeparr[54, 1] = GammaupdateAddr;
            GammaValueTeparr[55, 0] = 0x06003100;
            GammaValueTeparr[55, 1] = 0x80000;         //使写入Ram的Gamma生效
            //数据起始生效地址
            str += "0x0600309C,0x" + GammaupdateAddr.ToString("X8") + "\r\n";   //变更初始化后该地址可能发生变化
            str += "0x06003100,0x80000" + "\r\n";
            //txtGammaTest.Text = str;
            //txt3288Input.Text = str ;
            //btnCodeConvert_Click(null,null);
            //btnCmdSendX_Click(null,null);
        }
        private void HexStringToByteArray(string s, out byte[] Teparr, out byte[] TepArr2, out byte[] TepArr3, out byte[] TepArr4)
        {
            //Regdata = arr.Trim().Split(',');  //字符串拆分成数组
            s = s.Replace(" ", "");
            s = s.Replace("\r\n", ",");
            s = s.TrimEnd('\r', '\n');
            s = s.TrimEnd(',');

            string result1 = s.Substring(0, 14 * 3 * 17); result1 = result1.TrimEnd(',');
            string result2 = s.Substring(14 * 3 * 17, 14 * 3 * 17); result2 = result2.TrimEnd(',');
            string result3 = s.Substring(14 * 3 * 17 * 2, 14 * 3 * 17); result3 = result3.TrimEnd(',');
            string result4 = s.Substring(14 * 3 * 17 * 3);

            Regdata = result1.Trim().Split(',');
            Teparr = new byte[Regdata.Length];
            for (int i = 0; i < Regdata.Length; i++)
            {
                Teparr[i] = Convert.ToByte(Regdata[i], 16);
            }

            Regdata = result2.Trim().Split(',');
            TepArr2 = new byte[Regdata.Length];
            for (int i = 0; i < Regdata.Length; i++)
            {
                TepArr2[i] = Convert.ToByte(Regdata[i], 16);
            }

            Regdata = result3.Trim().Split(',');
            TepArr3 = new byte[Regdata.Length];
            for (int i = 0; i < Regdata.Length; i++)
            {
                TepArr3[i] = Convert.ToByte(Regdata[i], 16);
            }

            Regdata = result4.Trim().Split(',');
            TepArr4 = new byte[Regdata.Length];
            for (int i = 0; i < Regdata.Length; i++)
            {
                TepArr4[i] = Convert.ToByte(Regdata[i], 16);
            }
            //return Teparr;
        }
        private void HexStringToByteArrayOne(string s, out byte[] Teparr)
        {
            //Regdata = arr.Trim().Split(',');  //字符串拆分成数组
            s = s.Replace(" ", "");
            s = s.Replace("\r\n", ",");
            s = s.TrimEnd('\r', '\n');
            s = s.TrimEnd(',');

            Regdata = s.Trim().Split(',');
            Teparr = new byte[Regdata.Length];
            for (int i = 0; i < Regdata.Length; i++)
            {
                Teparr[i] = Convert.ToByte(Regdata[i], 16);
            }
        }
        private void GammaWritePrepre()
        {
            uint[] GammaPrepare; uint[] GammaPrepare_C11;
            GammaPrepare_C11 = new uint[] {
                0X0B326FA0, 0X00000000,
                0X0B326FA4, 0X00000000,
                0X0B326FA8, 0X00B00000,
                0X0B326FAC, 0X00020000,
                0X0B326FB0, 0X08000040,
                0X0B326FB4, 0X00B00B00,
                0X0B326FB8, 0X00010000,
                0X0B326FBC, 0X04000020,
                0X0B326FC0, 0X00401600,
                0X0B326FC4, 0X00008000,
                0X0B326FC8, 0X02000010,
                0X0B326FCC, 0X00A01A00,
                0X0B326FD0, 0X00008000,
                0X0B326FD4, 0X02000010,
                0X0B326FD8, 0X00C02400,
                0X0B326FDC, 0X00008000,
                0X0B326FE0, 0X02000010,
                0X0B326FE4, 0X00C03000,
                0X0B326FE8, 0X00008000,
                0X0B326FEC, 0X02000010,
                0X0B326FF0, 0X00C03C00,
                0X0B326FF4, 0X00008000,
                0X0B326FF8, 0X02000010,
                0X0B326FFC, 0X00804800,
                0X0B327000, 0X00004000,
                0X0B327004, 0X01000008,
                0X0B327008, 0X00000000,
                0X0B32700C, 0X00004000,
                0X0B327010, 0X01000008,
                0X0B327014, 0X00B00000,
                0X0B327018, 0X00004000,
                0X0B32701C, 0X01000008,
                0X0B327020, 0X01600000,
                0X0B327024, 0X00004000,
                0X0B327028, 0X01000008,
                0X0B32702C, 0X01A00000,
                0X0B327030, 0X00004000,
                0X0B327034, 0X01000008,
                0X0B327038, 0X02400000,
                0X0B32703C, 0X00002000,
                0X0B327040, 0X00800004,
                0X0B327044, 0X03000000,
                0X0B327048, 0X00001000,
                0X0B32704C, 0X00400002,
                0X0B327050, 0X03C00000,
                0X0B327054, 0X00001000,
                0X0B327058, 0X00400002,
                0X0B32705C, 0X04800000,
                0X0B327060, 0X00000800,
                0X0B327064, 0X00200001,
                0X0B327068, 0X00000000,
                0X0B32706C, 0X00001000,
                0X0B327070, 0X00400002,
                0X0B327074, 0X00000000,
                0X0B327078, 0X00002000,
                0X0B32707C, 0X00800004,
                0X0B327080, 0X00000000,
                0X0B327084, 0X00000000,
                0X0B327088, 0X00000000,
                0X0B32708C, 0X00000000,
                0X0B327090, 0X00000000,
                0X0B327094, 0X00000000,
                0X0B327098, 0X00000000,
                0X0B32709C, 0X00000000,
                0X0B3270A0, 0X00000000,
                0X0B3270A4, 0X00000000,
                0X0B3270A8, 0X00000000,
                0X0B3270AC, 0X00000000,
                0X0B3270B0, 0X00000000,
                0X0B3270B4, 0X00000000,
                0X0B3270B8, 0X00000000,
                0X0B3270BC, 0X00000000,
                0X0B3270C0, 0X00000000,
                0X0B3270C4, 0X00000000,
                0X0B3270C8, 0X00000000,
                0X0B3270CC, 0X00000000,
                0X0B3270D0, 0X00000000,
                0X0B3270D4, 0X00000000,
                0X0B3270D8, 0X00000000,
                0X0B3270DC, 0X00000000,
                0X0B3270E0, 0X00000000,
                0X0B3270E4, 0X00000000,
                0X0B3270E8, 0X00000000,
                0X0B3270EC, 0X00000000,
                0X0B3270F0, 0X00000000,
                0X0B3270F4, 0X00000000,
                0X0B3270F8, 0X00000000,
                0X0B3270FC, 0X00000000,
                0X0B327100, 0X00000000,
                0X0B327104, 0X00000000,
                0X0B327108, 0X00000000,
                0X0B32710C, 0X00000000,
                0X0B327110, 0X00000000,
                0X0B327114, 0X00000000,
                0X0B327118, 0X00000000,
                0X0B32711C, 0X00000000,
                0X0B327120, 0X00000000,
                0X0B327124, 0X00000000,
                0X0B327128, 0X00000000,
                0X0B32712C, 0X00000000,
                0X0B327130, 0X00000000,
                0X0B327134, 0X00000000,
                0X0B327138, 0X00000000,
                0X0B32713C, 0X00000000,
                0X0B327140, 0X00000000,
                0X0B327144, 0X00000000,
                0X0B327148, 0X00000000,
                0X0B32714C, 0X00000000,
                0X0B327150, 0X00242424,
                0X0B327154, 0X24240000,
                0X0B327158, 0X00000024,
                0X0B32715C, 0X00242424,
                0X0B327160, 0X24240000,
                0X0B327164, 0X00000024,
                0X0B327168, 0X00242424,
                0X0B32716C, 0X24240000,
                0X0B327170, 0X00000024,
                0X0B327174, 0X00242424,
                0X0B327178, 0X24240000,
                0X0B32717C, 0X00000024,
                0X0B327180, 0X00242424,
                0X0B327184, 0X00000000,
                0X0B327188, 0X00000000,
                0X0B32718C, 0X00000000,
                0X0B327190, 0X24240000,
                0X0B327194, 0X00000024,
                0X0B327198, 0X00242424,
                0X0B32719C, 0X24240000,
                0X0B3271A0, 0X00000024,
                0X0B3271A4, 0X00242424,
                0X0B3271A8, 0X24240000,
                0X0B3271AC, 0X00000024,
                0X0B3271B0, 0X00242424,
                0X0B3271B4, 0X24240000,
                0X0B3271B8, 0X00000024,
                0X0B3271BC, 0X00242424,
                0X0B3271C0, 0X24240000,
                0X0B3271C4, 0X00000024,
                0X0B3271C8, 0X00000000,
                0X0B3271CC, 0X00000000,
                0X0B3271D0, 0X00000000,
                0X0B3271D4, 0X00242424,
                0X0B3271D8, 0X24240000,
                0X0B3271DC, 0X00000024,
                0X0B3271E0, 0X00242424,
                0X0B3271E4, 0X24240000,
                0X0B3271E8, 0X00000024,
                0X0B3271EC, 0X00000000,
                0X0B3271F0, 0X00000000,
                0X0B3271F4, 0X00000000,
                0X0B3271F8, 0X00000000,
                0X0B3271FC, 0X00000000,
                0X0B327200, 0X00000000,
                0X0B327204, 0X00000000,
                0X0B327208, 0X00000000,
                0X0B32720C, 0X00000000,
                0X0B327210, 0X00000000,
                0X0B327214, 0X00000000,
                0X0B327218, 0X00000000,
                0X0B32721C, 0X00000000,
                0X0B327220, 0X00000000,
                0X0B327224, 0X00000000,
                0X0B327228, 0X00000000,
                0X0B32722C, 0X00000000,
                0X0B327230, 0X00000000,
                0X0B327234, 0X00000000,
                0X0B327238, 0X00000000,
                0X0B32723C, 0X00000000,
                0X0B327240, 0X00000000,
                0X0B327244, 0X00000000,
                0X0B327248, 0X00000000,
                0X0B32724C, 0X00000000,
                0X0B327250, 0X00000000,
                0X0B327254, 0X00000000,
                0X0B327258, 0X00000000,
                0X0B32725C, 0X00000000,
                0X0B327260, 0X00000000,
                0X0B327264, 0X00000000,
                0X0B327268, 0X00000000,
                0X0B32726C, 0X00000000,
                0X0B327270, 0X00000000,
                0X0B327274, 0X00000000,
                0X0B327278, 0X00000000,
                0X0B32727C, 0X00000000,
                0X0B327280, 0X00000000,
                0X0B327284, 0X00000000,
                0X0B327288, 0X00000000,
                0X0B32728C, 0X00000000,
                0X0B327290, 0X00000000,
                0X0B327294, 0X00000000,
                0X0B327298, 0X00000000,
                0X0B32729C, 0X00000000,
                0X0B3272A0, 0X00000000,
                0X0B3272A4, 0X00000000,
                0X0B3272A8, 0X00000000,
                0X0B3272AC, 0X00000000,
                0X0B3272B0, 0X00000000,
                0X0B3272B4, 0X00000000,
                0X0B3272B8, 0X00000000,
                0X0B3272BC, 0X00000000,
                0X0B3272C0, 0X00000000,
                0X0B3272C4, 0X00000000,
                0X0B3272C8, 0X00000000,
                0X0B3272CC, 0X00000000,
                0X0B3272D0, 0X00000000,
                0X0B3272D4, 0X00000000,
                0X0B3272D8, 0X00000000,
                0X0B3272DC, 0X00000000,
                0X0B3272E0, 0X00000000,
                0X0B3272E4, 0X00000000,
                0X0B3272E8, 0X00000000,
                0X0B3272EC, 0X00000000,
                0X0B3272F0, 0X00000000,
                0X0B3272F4, 0X00000000,
                0X0B3272F8, 0X00000000,
                0X0B3272FC, 0X00000000,
                0X0B327300, 0X00000000,
                0X0B327304, 0X00000000,
                0X0B327308, 0X00000000,
                0X0B32730C, 0X00000000,
                0X0B327310, 0X00000000,
                0X0B327314, 0X00000000,
                0X0B327318, 0X00000000,
                0X0B32731C, 0X00000000,
                0X0B327320, 0X00000000,
                0X0B327324, 0X00000000,
                0X0B327328, 0X00000000,
                0X0B32732C, 0X00000000,
                0X0B327330, 0X00000000,
                0X0B327334, 0XB0000040,
                0X0B327338, 0X00000018,
                0X0B32733C, 0X8B0003BC,
                0X0B327340, 0X90006441,
                0X0B327344, 0X00000032,
                0X0B327348, 0XC2000C14,
                0X0B32734C, 0X100160C6,
                0X0B327350, 0X000000DC,
                0X0B327354, 0XD0001610,
                0X0B327358, 0X00022E0F,
                0X0B32735C, 0X000000FD,
                0X0B327360, 0XD00036EC,
                0X0B327364, 0X0003FC0F,
                0X0B327368, 0X000000FD,
                0X0B32736C, 0XD0003FFC,
                0X0B327370, 0X0000000F,
                0X0B327374, 0X00000000,
                0X0B327378, 0X8B000004,
                0X0B32737C, 0XB0003BC1,
                0X0B327380, 0X00000018,
                0X0B327384, 0X29000644,
                0X0B327388, 0X9000C143,
                0X0B32738C, 0X00000069,
                0X0B327390, 0XC100160C,
                0X0B327394, 0XC001610D,
                0X0B327398, 0X000000FD,
                0X0B32739C, 0XDC0022E0,
                0X0B3273A0, 0XC0036ECF,
                0X0B3273A4, 0X000000FD,
                0X0B3273A8, 0XDC003FC0,
                0X0B3273AC, 0XC003FFCF,
                0X0B3273B0, 0X000000FD,
                0X0B3273B4, 0X00000000,
                0X0B3273B8, 0X00005540,
                0X0B3273BC, 0X000000F4,
                0X0B3273C0, 0X40001000,
                0X0B3273C4, 0X0003FFCF,
                0X0B3273C8, 0X000000F4,
                0X0B3273CC, 0X00000000,
                0X0B3273D0, 0X00000000,
                0X0B3273D4, 0X00000000,
                0X0B3273D8, 0X00000000,
                0X0B3273DC, 0X00000000,
                0X0B3273E0, 0X00000000,
                0X0B3273E4, 0X00000000,
                0X0B3273E8, 0X00000000,
                0X0B3273EC, 0X00000000,
                0X0B3273F0, 0X00000000,
                0X0B3273F4, 0X00000000,
                0X0B3273F8, 0X00000000,
                0X0B3273FC, 0X00000000,
                0X0B327400, 0X00000000,
                0X0B327404, 0X00000000,
                0X0B327408, 0X00000000,
                0X0B32740C, 0X00000000,
                0X0B327410, 0X00000000,
                0X0B327414, 0X00000000,
                0X0B327418, 0X00000000,
                0X0B32741C, 0X00000000,
                0X0B327420, 0X00000000,
                0X0B327424, 0X00000000,
                0X0B327428, 0X00000000,
                0X0B32742C, 0X00000000,
                0X0B327430, 0X00000000,
                0X0B327434, 0X00000000,
                0X0B327438, 0X00000000,
                0X0B32743C, 0X00000000,
                0X0B327440, 0X00000000,
                0X0B327444, 0X00000000,
                0X0B327448, 0X00000000,
                0X0B32744C, 0X00000000,
                0X0B327450, 0X00000000,
                0X0B327454, 0X00000000,
                0X0B327458, 0X00000000,
                0X0B32745C, 0X00000000,
                0X0B327460, 0X00000000,
                0X0B327464, 0X00000000,
                0X0B327468, 0X00000000,
                0X0B32746C, 0X00000000,
                0X0B327470, 0X00000000,
                0X0B327474, 0X00000000,
                0X0B327478, 0X00000000,
                0X0B32747C, 0X00000000,
                0X0B327480, 0X00000000,
                0X0B327484, 0X00000000,
                0X0B327488, 0X00000000,
                0X0B32748C, 0X00000000,
                0X0B327490, 0X00000000,
                0X0B327494, 0X00000000,
                0X0B327498, 0X00000000,
                0X0B32749C, 0X00000000,
                0X0B3274A0, 0X00000000,
                0X0B3274A4, 0X00000000,
                0X0B3274A8, 0X00000000,
                0X0B3274AC, 0X00000000,
                0X0B3274B0, 0X00000000,
                0X0B3274B4, 0X00000000,
                0X0B3274B8, 0X00000000,
                0X0B3274BC, 0X00000000,
                0X0B3274C0, 0X00000000,
                0X0B3274C4, 0X00000000,
                0X0B3274C8, 0X00000000,
                0X0B3274CC, 0X00000000,
                0X0B3274D0, 0X00000000,
                0X0B3274D4, 0X00000000,
                0X0B3274D8, 0X00000000,
                0X0B3274DC, 0X00000000,
                0X0B3274E0, 0X00000000,
                0X0B3274E4, 0X00000000,
                0X0B3274E8, 0X00000000,
                0X0B3274EC, 0X00000000,
                0X0B3274F0, 0X00000000,
                0X0B3274F4, 0X00000000,
                0X0B3274F8, 0X00000000,
                0X0B3274FC, 0X00000000,
                0X0B327500, 0X00000000,
                0X0B327504, 0X00000000,
                0X0B327508, 0X00000000,
                0X0B32750C, 0X00000000,
                0X0B327510, 0X00000000,
                0X0B327514, 0X00100100,
                0X0B327518, 0X00000001,
                0X0B32751C, 0X30030030,
                0X0B327520, 0X00700700,
                0X0B327524, 0X00000007,
                0X0B327528, 0XB00B00B0,
                0X0B32752C, 0X00F00F00,
                0X0B327530, 0X0000000F,
                0X0B327534, 0X30130130,
                0X0B327538, 0X01701701,
                0X0B32753C, 0X00000017,
                0X0B327540, 0XF01F01F0,
                0X0B327544, 0X02702701,
                0X0B327548, 0X00000027,
                0X0B32754C, 0XF02F02F0,
                0X0B327550, 0X03703702,
                0X0B327554, 0X00000037,
                0X0B327558, 0XF03F03F0,
                0X0B32755C, 0X04F04F03,
                0X0B327560, 0X0000004F,
                0X0B327564, 0XF06F06F0,
                0X0B327568, 0X08F08F06,
                0X0B32756C, 0X0000008F,
                0X0B327570, 0XF0CF0CF0,
                0X0B327574, 0X0EF0EF0C,
                0X0B327578, 0X000000EF,
                0X0B32757C, 0XF0FF0FF0,
                0X0B327580, 0X0FF0FF0F,
                0X0B327584, 0X000000FF,
                0X0B327588, 0XF0FF0FF0,
                0X0B32758C, 0X0FF0FF0F,
                0X0B327590, 0X000000FF,
                0X0B327594, 0XF0FF0FF0,
                0X0B327598, 0X0FF0FF0F,
                0X0B32759C, 0X000000FF,
                0X0B3275A0, 0XF0FF0FF0,
                0X0B3275A4, 0X0FF0FF0F,
                0X0B3275A8, 0X000000FF,
                0X0B3275AC, 0XF0FF0FF0,
                0X0B3275B0, 0X0FF0FF0F,
                0X0B3275B4, 0X000000FF,
                0X0B3275B8, 0XF0FF0FF0,
                0X0B3275BC, 0X0FF0FF0F,
                0X0B3275C0, 0X000000FF,
                0X0B3275C4, 0XF0FF0FF0,
                0X0B3275C8, 0X0FF0FF0F,
                0X0B3275CC, 0X000000FF,
                0X0B3275D0, 0XF0FF0FF0,
                0X0B3275D4, 0X0FF0FF0F,
                0X0B3275D8, 0X000000FF,
                0X0B3275DC, 0XF0FF0FF0,
                0X0B3275E0, 0X0FF0FF0F,
                0X0B3275E4, 0X000000FF,
            };
            GammaPrepare = new uint[] {
                //[BCMG_LUT]
                0X0B31C260,0X00000000,
                0X0B31C264,0X00000000,
                0X0B31C268,0X00A00000,
                0X0B31C26C,0X00020000,
                0X0B31C270,0X08000040,
                0X0B31C274,0X00A00A00,
                0X0B31C278,0X00010000,
                0X0B31C27C,0X04000020,
                0X0B31C280,0X00A01400,
                0X0B31C284,0X00008000,
                0X0B31C288,0X02000010,
                0X0B31C28C,0X00301E00,
                0X0B31C290,0X00004000,
                0X0B31C294,0X01000008,
                0X0B31C298,0X00C02100,
                0X0B31C29C,0X00004000,
                0X0B31C2A0,0X01000008,
                0X0B31C2A4,0X00C02D00,
                0X0B31C2A8,0X00004000,
                0X0B31C2AC,0X01000008,
                0X0B31C2B0,0X00C03900,
                0X0B31C2B4,0X00002000,
                0X0B31C2B8,0X00800004,
                0X0B31C2BC,0X00C04500,
                0X0B31C2C0,0X00002000,
                0X0B31C2C4,0X00800004,
                0X0B31C2C8,0X00000000,
                0X0B31C2CC,0X00002000,
                0X0B31C2D0,0X00800004,
                0X0B31C2D4,0X00A00000,
                0X0B31C2D8,0X00001000,
                0X0B31C2DC,0X00400002,
                0X0B31C2E0,0X01400000,
                0X0B31C2E4,0X00001000,
                0X0B31C2E8,0X00400002,
                0X0B31C2EC,0X01E00000,
                0X0B31C2F0,0X00000800,
                0X0B31C2F4,0X00200001,
                0X0B31C2F8,0X02100000,
                0X0B31C2FC,0X00001000,
                0X0B31C300,0X00400002,
                0X0B31C304,0X02D00000,
                0X0B31C308,0X00002000,
                0X0B31C30C,0X00800004,
                0X0B31C310,0X03900000,
                0X0B31C314,0X00000000,
                0X0B31C318,0X00000000,
                0X0B31C31C,0X04500000,
                0X0B31C320,0X00000000,
                0X0B31C324,0X00000000,
                0X0B31C328,0X00000000,
                0X0B31C32C,0X00000000,
                0X0B31C330,0X00000000,
                0X0B31C334,0X00000000,
                0X0B31C338,0X00000000,
                0X0B31C33C,0X00000000,
                0X0B31C340,0X00000000,
                0X0B31C344,0X00000000,
                0X0B31C348,0X00000000,
                0X0B31C34C,0X00000000,
                0X0B31C350,0X00000000,
                0X0B31C354,0X00000000,
                0X0B31C358,0X00000000,
                0X0B31C35C,0X00000000,
                0X0B31C360,0X00000000,
                0X0B31C364,0X00000000,
                0X0B31C368,0X00000000,
                0X0B31C36C,0X00000000,
                0X0B31C370,0X00000000,
                0X0B31C374,0X00000000,
                0X0B31C378,0X00000000,
                0X0B31C37C,0X00000000,
                0X0B31C380,0X00000000,
                0X0B31C384,0X00000000,
                0X0B31C388,0X00000000,
                0X0B31C38C,0X00000000,
                0X0B31C390,0X00000000,
                0X0B31C394,0X00000000,
                0X0B31C398,0X00000000,
                0X0B31C39C,0X00000000,
                0X0B31C3A0,0X00000000,
                0X0B31C3A4,0X00000000,
                0X0B31C3A8,0X00000000,
                0X0B31C3AC,0X00000000,
                0X0B31C3B0,0X00000000,
                0X0B31C3B4,0X00000000,
                0X0B31C3B8,0X00000000,
                0X0B31C3BC,0X00000000,
                0X0B31C3C0,0X00000000,
                0X0B31C3C4,0X00000000,
                0X0B31C3C8,0X00000000,
                0X0B31C3CC,0X00000000,
                0X0B31C3D0,0X00000000,
                0X0B31C3D4,0X00000000,
                0X0B31C3D8,0X00000000,
                0X0B31C3DC,0X00000000,
                0X0B31C3E0,0X00000000,
                0X0B31C3E4,0X00000000,
                0X0B31C3E8,0X00000000,
                0X0B31C3EC,0X00000000,
                0X0B31C3F0,0X00000000,
                0X0B31C3F4,0X00000000,
                0X0B31C3F8,0X00000000,
                0X0B31C3FC,0X00000000,
                0X0B31C400,0X00000000,
                0X0B31C404,0X00000000,
                0X0B31C408,0X00000000,
                0X0B31C40C,0X00000000,
                0X0B31C410,0X64000000,
                0X0B31C414,0X00006464,
                0X0B31C418,0X64646400,
                0X0B31C41C,0X64000000,
                0X0B31C420,0X00006464,
                0X0B31C424,0X64646400,
                0X0B31C428,0X64000000,
                0X0B31C42C,0X00006464,
                0X0B31C430,0X32323200,
                0X0B31C434,0X32000000,
                0X0B31C438,0X00003232,
                0X0B31C43C,0X32323200,
                0X0B31C440,0X00000000,
                0X0B31C444,0X00000000,
                0X0B31C448,0X00000000,
                0X0B31C44C,0X64000000,
                0X0B31C450,0X00006464,
                0X0B31C454,0X64646400,
                0X0B31C458,0X64000000,
                0X0B31C45C,0X00006464,
                0X0B31C460,0X64646400,
                0X0B31C464,0X64000000,
                0X0B31C468,0X00006464,
                0X0B31C46C,0X32323200,
                0X0B31C470,0X32000000,
                0X0B31C474,0X00003232,
                0X0B31C478,0X32323200,
                0X0B31C47C,0X00000000,
                0X0B31C480,0X00000000,
                0X0B31C484,0X00000000,
                0X0B31C488,0X64000000,
                0X0B31C48C,0X00006464,
                0X0B31C490,0X64646400,
                0X0B31C494,0X64000000,
                0X0B31C498,0X00006464,
                0X0B31C49C,0X64646400,
                0X0B31C4A0,0X64000000,
                0X0B31C4A4,0X00006464,
                0X0B31C4A8,0X32323200,
                0X0B31C4AC,0X32000000,
                0X0B31C4B0,0X00003232,
                0X0B31C4B4,0X32323200,
                0X0B31C4B8,0X00000000,
                0X0B31C4BC,0X00000000,
                0X0B31C4C0,0X00000000,
                0X0B31C4C4,0X00000000,
                0X0B31C4C8,0X00000000,
                0X0B31C4CC,0X00000000,
                0X0B31C4D0,0X00000000,
                0X0B31C4D4,0X2E2E0000,
                0X0B31C4D8,0XD5D5D52E,
                0X0B31C4DC,0XEA171717,
                0X0B31C4E0,0X0000EAEA,
                0X0B31C4E4,0XFFFFFF00,
                0X0B31C4E8,0X15FFFFFF,
                0X0B31C4EC,0XE8E81515,
                0X0B31C4F0,0X181818E8,
                0X0B31C4F4,0X40D1D1D1,
                0X0B31C4F8,0XBABA4040,
                0X0B31C4FC,0X555555BA,
                0X0B31C500,0X6AA2A2A2,
                0X0B31C504,0X8B8B6A6A,
                0X0B31C508,0X8080808B,
                0X0B31C50C,0X95747474,
                0X0B31C510,0X5D5D9595,
                0X0B31C514,0XAAAAAA5D,
                0X0B31C518,0XBF454545,
                0X0B31C51C,0X2E2EBFBF,
                0X0B31C520,0XD5D5D52E,
                0X0B31C524,0XEA171717,
                0X0B31C528,0X0000EAEA,
                0X0B31C52C,0XFFFFFF00,
                0X0B31C530,0X15FFFFFF,
                0X0B31C534,0XE8E81515,
                0X0B31C538,0X181818E8,
                0X0B31C53C,0X40D1D1D1,
                0X0B31C540,0XBABA4040,
                0X0B31C544,0X555555BA,
                0X0B31C548,0X6AA2A2A2,
                0X0B31C54C,0X8B8B6A6A,
                0X0B31C550,0X8080808B,
                0X0B31C554,0X95747474,
                0X0B31C558,0X5D5D9595,
                0X0B31C55C,0XAAAAAA5D,
                0X0B31C560,0XBF454545,
                0X0B31C564,0X2E2EBFBF,
                0X0B31C568,0XD5D5D52E,
                0X0B31C56C,0XEA171717,
                0X0B31C570,0X0000EAEA,
                0X0B31C574,0XFFFFFF00,
                0X0B31C578,0X15FFFFFF,
                0X0B31C57C,0XE8E81515,
                0X0B31C580,0X181818E8,
                0X0B31C584,0X40D1D1D1,
                0X0B31C588,0XBABA4040,
                0X0B31C58C,0X555555BA,
                0X0B31C590,0X6AA2A2A2,
                0X0B31C594,0X8B8B6A6A,
                0X0B31C598,0X8080808B,
                0X0B31C59C,0X95747474,
                0X0B31C5A0,0X5D5D9595,
                0X0B31C5A4,0XAAAAAA5D,
                0X0B31C5A8,0XBF454545,
                0X0B31C5AC,0X2E2EBFBF,
                0X0B31C5B0,0XD5D5D52E,
                0X0B31C5B4,0XEA171717,
                0X0B31C5B8,0X0000EAEA,
                0X0B31C5BC,0XFFFFFF00,
                0X0B31C5C0,0X15FFFFFF,
                0X0B31C5C4,0XE8E81515,
                0X0B31C5C8,0X181818E8,
                0X0B31C5CC,0X40D1D1D1,
                0X0B31C5D0,0XBABA4040,
                0X0B31C5D4,0X555555BA,
                0X0B31C5D8,0X6AA2A2A2,
                0X0B31C5DC,0X8B8B6A6A,
                0X0B31C5E0,0X8080808B,
                0X0B31C5E4,0X95747474,
                0X0B31C5E8,0X5D5D9595,
                0X0B31C5EC,0XAAAAAA5D,
                0X0B31C5F0,0X9A000004,
                0X0B31C5F4,0XA0003AC1,
                0X0B31C5F8,0X00000019,
                0X0B31C5FC,0X66000620,
                0X0B31C600,0XD000BD02,
                0X0B31C604,0X0000004C,
                0X0B31C608,0X9A001414,
                0X0B31C60C,0X30014189,
                0X0B31C610,0X000000FD,
                0X0B31C614,0XD30027C0,
                0X0B31C618,0X30036ECF,
                0X0B31C61C,0X000000FD,
                0X0B31C620,0XD3003E80,
                0X0B31C624,0X3003FFFF,
                0X0B31C628,0X000000FD,
                0X0B31C62C,0X9A000007,
                0X0B31C630,0XA0003AF1,
                0X0B31C634,0X00000019,
                0X0B31C638,0X66000623,
                0X0B31C63C,0XD000BD32,
                0X0B31C640,0X0000004C,
                0X0B31C644,0X9A001417,
                0X0B31C648,0X300141B9,
                0X0B31C64C,0X000000FD,
                0X0B31C650,0XD30027C3,
                0X0B31C654,0X30036EFF,
                0X0B31C658,0X000000FD,
                0X0B31C65C,0XD3003E83,
                0X0B31C660,0X3003FFBF,
                0X0B31C664,0X000000FD,
                0X0B31C668,0X9A000007,
                0X0B31C66C,0XA0003AF1,
                0X0B31C670,0X00000019,
                0X0B31C674,0X66000623,
                0X0B31C678,0XD000BD32,
                0X0B31C67C,0X0000004C,
                0X0B31C680,0X9A001417,
                0X0B31C684,0X300141B9,
                0X0B31C688,0X000000FD,
                0X0B31C68C,0XD30027C3,
                0X0B31C690,0X30036EFF,
                0X0B31C694,0X000000FD,
                0X0B31C698,0XD3003E83,
                0X0B31C69C,0X3003FFBF,
                0X0B31C6A0,0X000000FD,
                0X0B31C6A4,0X85000293,
                0X0B31C6A8,0X5000CCFF,
                0X0B31C6AC,0X000000F8,
                0X0B31C6B0,0X85003FFF,
                0X0B31C6B4,0X6000020F,
                0X0B31C6B8,0X00000012,
                0X0B31C6BC,0XE1000060,
                0X0B31C6C0,0X80000C41,
                0X0B31C6C4,0X00000030,
                0X0B31C6C8,0X47000188,
                0X0B31C6CC,0X00003005,
                0X0B31C6D0,0X00000099,
                0X0B31C6D4,0XBD0004C8,
                0X0B31C6D8,0XE00051CE,
                0X0B31C6DC,0X000000FA,
                0X0B31C6E0,0XAE000CCC,
                0X0B31C6E4,0XE001857F,
                0X0B31C6E8,0X000000FA,
                0X0B31C6EC,0XAE001FFC,
                0X0B31C6F0,0XE002BD4F,
                0X0B31C6F4,0X000000FA,
                0X0B31C6F8,0XAE003FFC,
                0X0B31C6FC,0X6000020F,
                0X0B31C700,0X00000012,
                0X0B31C704,0XE1000060,
                0X0B31C708,0X80000C41,
                0X0B31C70C,0X00000030,
                0X0B31C710,0X47000188,
                0X0B31C714,0X00003005,
                0X0B31C718,0X00000099,
                0X0B31C71C,0XBD0004C8,
                0X0B31C720,0XE00051CE,
                0X0B31C724,0X000000FA,
                0X0B31C728,0XAE000CCC,
                0X0B31C72C,0XE001857F,
                0X0B31C730,0X000000FA,
                0X0B31C734,0XAE001FFC,
                0X0B31C738,0XE002BD4F,
                0X0B31C73C,0X000000FA,
                0X0B31C740,0XAE003FFC,
                0X0B31C744,0X6000020F,
                0X0B31C748,0X00000012,
                0X0B31C74C,0XE1000060,
                0X0B31C750,0X80000C41,
                0X0B31C754,0X00000030,
                0X0B31C758,0X47000188,
                0X0B31C75C,0X00003005,
                0X0B31C760,0X00000099,
                0X0B31C764,0XBD0004C8,
                0X0B31C768,0XE00051CE,
                0X0B31C76C,0X000000FA,
                0X0B31C770,0XAE000CCC,
                0X0B31C774,0XE001857F,
                0X0B31C778,0X000000FA,
                0X0B31C77C,0XAE001FFC,
                0X0B31C780,0XE002BD4F,
                0X0B31C784,0X000000FA,
                0X0B31C788,0XAE003FFC,
                0X0B31C78C,0X6000020F,
                0X0B31C790,0X00000012,
                0X0B31C794,0XE1000060,
                0X0B31C798,0X80000C41,
                0X0B31C79C,0X00000030,
                0X0B31C7A0,0X47000188,
                0X0B31C7A4,0X00003005,
                0X0B31C7A8,0X00000099,
                0X0B31C7AC,0XBD0004C8,
                0X0B31C7B0,0XE00051CE,
                0X0B31C7B4,0X000000FA,
                0X0B31C7B8,0XAE000CCC,
                0X0B31C7BC,0XE001FFCF,
                0X0B31C7C0,0X000000FA,
                0X0B31C7C4,0XAE002BD4,
                0X0B31C7C8,0XE003FFCF,
                0X0B31C7CC,0X000000FA,
                0X0B31C7D0,0X00000000,
                0X0B31C7D4,0X00100100,
                0X0B31C7D8,0X00000001,
                0X0B31C7DC,0X30030030,
                0X0B31C7E0,0X00700700,
                0X0B31C7E4,0X00000007,
                0X0B31C7E8,0XF00F00F0,
                0X0B31C7EC,0X01701700,
                0X0B31C7F0,0X00000017,
                0X0B31C7F4,0XF01F01F0,
                0X0B31C7F8,0X02F02F01,
                0X0B31C7FC,0X0000002F,
                0X0B31C800,0XF03F03F0,
                0X0B31C804,0X04F04F03,
                0X0B31C808,0X0000004F,
                0X0B31C80C,0XF06F06F0,
                0X0B31C810,0X08F08F06,
                0X0B31C814,0X0000008F,
                0X0B31C818,0XF0CF0CF0,
                0X0B31C81C,0X0EF0EF0C,
                0X0B31C820,0X000000EF,
                0X0B31C824,0XF0FF0FF0,
                0X0B31C828,0X0FF0FF0F,
                0X0B31C82C,0X000000FF,
                0X0B31C830,0XF0FF0FF0,
                0X0B31C834,0X0FF0FF0F,
                0X0B31C838,0X000000FF,
                0X0B31C83C,0XF0FF0FF0,
                0X0B31C840,0X0FF0FF0F,
                0X0B31C844,0X000000FF,
                0X0B31C848,0XF0FF0FF0,
                0X0B31C84C,0X0FF0FF0F,
                0X0B31C850,0X000000FF,
                0X0B31C854,0XF0FF0FF0,
                0X0B31C858,0X0FF0FF0F,
                0X0B31C85C,0X000000FF,
                0X0B31C860,0XF0FF0FF0,
                0X0B31C864,0X0FF0FF0F,
                0X0B31C868,0X000000FF,
                0X0B31C86C,0XF0FF0FF0,
                0X0B31C870,0X0FF0FF0F,
                0X0B31C874,0X000000FF,
                0X0B31C878,0XF0FF0FF0,
                0X0B31C87C,0X0FF0FF0F,
                0X0B31C880,0X000000FF,
                0X0B31C884,0XF0FF0FF0,
                0X0B31C888,0X0FF0FF0F,
                0X0B31C88C,0X000000FF,
                0X0B31C890,0XF0FF0FF0,
                0X0B31C894,0X0FF0FF0F,
                0X0B31C898,0X000000FF,
                0X0B31C89C,0XF0FF0FF0,
                0X0B31C8A0,0X0FF0FF0F,
                0X0B31C8A4,0X000000FF,
                 };
            if (rdoModelC11.Checked) { HisiReg32Write(GammaPrepare_C11); }
            else if (rdoModelD17.Checked)
            {
                HisiReg32Write(GammaPrepare);
            }
        }
        private void WriteArr32(uint[] Gammaarr)
        {
            string str1 = "", str;
            uint HiReg, HiData;
            txt3288Output.Text = "";
            byte SecondByte_Add;
            for (int tt = 0; tt < Gammaarr.Length; tt += 2)
            {
                {
                    HiReg = Gammaarr[tt];
                    HiData = Gammaarr[tt + 1];
                    SecondByte_Add = (byte)((HiReg >> 8) & 0xFF);
                    if ((SecondByte_Add == 0x2C) || (SecondByte_Add == 0x3C))
                    {
                        MessageBox.Show("数据发送异常,请确认是否存在2C/3C", "提示："); return;
                        //为简化流程放弃不对2C、3C做处理。
                        /*
                            FF,FE,29,FF,02,00,88,29,2C,1F,00,00,00
                            Change to:
                            FF,FE,FD,29
                            FF,02,00,88
                            2C,1F,00,00,00                            */
                        str = "4," + "FF,FE,FD,29\r\n" + "4," + String.Format("FF,{0:X2},{1:X2},{2:X2}\r\n", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                        str += "5," + String.Format("{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                    }
                    else
                    {
                        str = "0D," + String.Format("FF,FE,29,FF,{0:X2},{1:X2},{2:X2},", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                        str += String.Format("39,{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                    }
                    str1 += str + "\r\n";
                }
            }//转换为下位机支持的非常规方式
            //txt3288Output.Text = str1;
            byte[] TepArr = new byte[] { 255 };
            HexStringToByteArrayOne(str1, out TepArr);
            try
            {
                frmTop.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);
            }
            catch
            {
                showmsg("串口异常，请手动退出");
                return;
            }
        }
        private void GammaValueSet(uint[,] Gammaarr)  //把拆分后Gamma值按照DDIC 规定格式排列后送入DDIC
        {
            uint[] Tep32Arr;
            Tep32Arr = new uint[Gammaarr.Length];
            for (int tt = 0; tt < Gammaarr.Length / 2; tt++)
            {
                Tep32Arr[tt * 2] = Gammaarr[tt, 0];
                Tep32Arr[tt * 2 + 1] = Gammaarr[tt, 1];
            }//把GammaValue由二维数组转换为一维数组
            HisiReg32Write(Tep32Arr);
        }
        private void GammaValueSet_index(uint[,] Gammaarr)  //把拆分后Gamma值按照DDIC 规定格式排列后送入DDIC
        {
            uint[] Tep32Arr;
            Tep32Arr = new uint[Gammaarr.Length];
            for (int tt = 0; tt < Gammaarr.Length / 2; tt++)
            {
                Tep32Arr[tt * 2] = Gammaarr[tt, 0];
                Tep32Arr[tt * 2 + 1] = Gammaarr[tt, 1];
            }//把GammaValue由二维数组转换为一维数组
            uint[] TepReg = new uint[] { 255 };
            uint[] TepReg1 = new uint[] { 255 };
            //index = 2;

            TepReg = Tep32Arr.Skip(((GPN - index) / 2) * 3 * 2).Take(6).ToArray(); // 1s：开始下标； 2e：
            TepReg1 = Tep32Arr.Skip(108).Take(4).ToArray(); // 1s：开始下标； 2e：结束下标
            TepReg = TepReg.Concat(TepReg1).ToArray();
            WriteArr32(TepReg);
        }
        private void HisiReg32Write(uint[] HiReg32_Data32Arr)
        {
            uint[] TepReg = new uint[] { 255 };
            int x = HiReg32_Data32Arr.Length;
            //TepReg = GammaPrepare32.Skip(0).Take(200).ToArray(); // 1s：开始下标； 2e：结束下标
            int step = 72;  //必须是偶数  且step*14/2+6必须小于512
            int tep = x / step;
            int tt = 0;
            for (tt = 0; tt < tep; tt++)
            {
                TepReg = HiReg32_Data32Arr.Skip(step * tt).Take(step).ToArray(); // 1s：开始下标； 2e：结束下标
                WriteArr32(TepReg);
            }
            if ((x % step) != 0)
            {
                TepReg = HiReg32_Data32Arr.Skip(step * tt).ToArray(); // 从x到结束
                WriteArr32(TepReg);
            }
        }
        private void btnAutoGammas_Click(object sender, EventArgs e)
        {
            if (frmMain.frmain.btnCal_manual.Tag.ToString() != "0calDone")
            {
                frmMain.frmain.btnCal_manual.Enabled = true;
                MessageBox.Show("需要提前连接410并校准~", "提示：");
                return;
            }
            txtGammaNode_TextChanged(null, null);
            numStep_ValueChanged(null, null);
            int flag = 1;
            Gammnum = (byte)numGammaPage.Value;
            JumpGammaAdjustFlag = false;
            txttip.ForeColor = Color.Black;
            STOPAutoGammaFlag = false;
            GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[GPN, 3];
            D_lum = new double[GPN];
            index = 1;  //Gamma绑点序号，第一个绑点通常对应255灰阶
            flag1 = flag2 = flag3 = 1;
            rstep = 16;
            gstep = 16;
            bstep = 16;
            txttip.Text += "GammaValue最大值被限定为: " + GammaValueMax + "\r\n";
            TimTest.Start();
            TimeNow = DateTime.Now; //开始计时

            for (int i = 0; i < GPN; i++)  //计算相应绑点对应的亮度值
            {
                D_lum[i] = (Math.Pow((Point_Hi3288[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
            }

            D_x = Convert.ToDouble(txtVT_x.Text);  //获取调节目标色坐标 x
            D_y = Convert.ToDouble(txtVT_y.Text); ////获取调节目标色坐标 y
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < GPN; i++)   // 通过色坐标已经亮度计算各绑点R/G/B分量对应的亮度值
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }
            labTargetlum.Text = "Tar:" + D_lum[GPN - index].ToString("f4"); ;   //显示当前绑点对应的目标亮度值
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        //lum_off = Convert.ToDouble(txtVT_loff.Text);
                        x_off = Convert.ToDouble(txtVT_xoff.Text);   //获取色坐标允差范围
                        y_off = Convert.ToDouble(txtVT_yoff.Text);
                        numPatternR.Value = Point_Hi3288[GPN - index];     //设定绑点灰阶
                        //numPatternG.Value = Point_Hi3288[GPN - index];
                        //numPatternB.Value = Point_Hi3288[GPN - index];
                        OLED_Show_Click(btnPatternW, e);       //显示255白画面，从255白画面开始调节
                        Thread.Sleep(200);
                        Msr_CA310();
                        Msr_CA310(); //修复讨厌的灰阶跳变时CA410数据异常的问题
                        ReceivOKFlag = false;
                        T17.Enabled = true;   //通过T17定时器进行Gamma调整
                        GammaTuning();
                    }
                    break;
                case 2:  //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private void Msr_CA310()
        {
            frmTop.Msr_CASDK();
            auto_lum = frmTop.CASDK_LUM;
            auto_x = frmTop.CASDK_x;
            auto_y = frmTop.CASDK_y;
        }   //获取CA310/CA410读数
        private void btnAutoGammaJumpTest_Click(object sender, EventArgs e)
        {
            JumpGammaAdjustFlag = true;
        }
        bool JumpGammaAdjustFlag = false;  //跳过某个绑点标志位，通常用于末尾低灰阶
        uint timeout = 0;
        private void GammaTuning()   //AutoGamma处理过程
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            T17.Enabled = false;
            T17.Interval = 20;  //不能过短，要给显示画面一个稳定的时间
            while (index <= GPN)
            {
                Msr_CA310();
                txtlvshow.Text = "Lv: " + auto_lum.ToString("#0.000000").Substring(0, 6) + "\r\n" + "x: " + auto_x.ToString("f4") + "\r\n" + "y: " + auto_y.ToString("f4") + "\r\n";
                labTargetlum.Text = "Tar:" + D_lum[GPN - index].ToString("#0.000000").Substring(0, 6);   // 在UI上显示当前绑点的目标亮度值
                txttip.Text += "Adjust inex:" + GPN + "----" + index + "\r\n";
                lum_off = (D_lum[GPN - index] * Convert.ToDouble(numVT_loff.Value) / 1000);  //计算当前绑点允许的亮度误差范围
                x_off = Convert.ToDouble(txtVT_xoff.Text);
                y_off = Convert.ToDouble(txtVT_yoff.Text);       //获取色坐标允许误差范围
                if (index > 20)  //根据灰阶范围修正色坐标误差范围
                {   //根据灰阶范围修正色坐标误差范围
                    if (D_lum[GPN - 1] < 10)
                    {
                        x_off += x_off * 2;
                        y_off += y_off * 2;
                    }
                    else if (D_lum[GPN - 1] < 30)
                    {
                        {
                            x_off += x_off * 2;
                            y_off += y_off * 2;
                        }
                    }
                    else if (D_lum[GPN - 1] < 90)
                    {
                        {
                            x_off += x_off * 1.5;
                            y_off += y_off * 1;
                        }
                    }
                    else
                    {
                        //{
                        //    x_off += x_off;
                        //    y_off += y_off;
                        //}
                    }
                } //根据灰阶范围修正色坐标误差范围
                if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                    flag1 = 0;  //判断亮度以及色坐标是否在目标范围
                if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                    flag2 = 0;  //判断亮度以及色坐标是否在目标范围
                if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                    flag3 = 0;  //判断亮度以及色坐标是否在目标范围
                if ((flag1 == 0 && flag2 == 0 && flag3 == 0) && (index < 10)) //低灰阶本来就很难调就不这么折腾了
                {   //初次判断调整OK后，再来复测一次，以消除偶发调不准问题
                    Msr_CA310();
                    if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                        flag1 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag1 = 1;
                    if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                        flag2 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag2 = 1;
                    if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                        flag3 = 0;  //判断亮度以及色坐标是否在目标范围
                    else flag3 = 1;
                }//初次判断调整OK后，再来复测一次，以消除偶发调不准问题,

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                if (((flag1 == 0 && flag2 == 0 && flag3 == 0) || JumpGammaAdjustFlag) || (timeout > 3 * numTurnGammaTimeout.Value))
                {
                    if (timeout > 3 * numTurnGammaTimeout.Value) { txttip.Text = "--模糊处理--\r\n" + "----模糊处理----\r\n" + "--------模糊处理--------\r\n" + "----模糊处理----\r\n" + "--模糊处理--\r\n"; }
                    timeout = 0;
                    index++;
                    if (index == GPN)   //  最后一个绑点通常为0，不用调试结束即可
                    {
                        TimTest.Stop();//停止计时
                        GammatuningOKFlag = true;
                        showmsg("Hi3288 gamma调试完毕!\r\n");
                        STOPAutoGammaFlag = true;
                        btn_Hi3288_Stop.PerformClick();
                        System.Media.SystemSounds.Beep.Play();
                        return;
                    }
                    if (index < GPN)
                    {
                        txttip.Text += index + "完成...\r\n";
                        txttip.Select(txttip.Text.Length, 0);
                        txttip.ScrollToCaret();    //上滚提示信息以便于查看
                        for (int i = 0; i < 25; i++)    //本意用于显示当前绑点目标信息，但一般用不到
                        {
                            //Convert.ToString(要转的数, 16);//string s = string.Format("0x{0:X2}", i);    // s = 0x0F;
                            //Points_LV.Text += (i + 1) + ",R/G/B, V" + Point_Hi3288[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                            //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                            //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";

                            //this.GammaReg_dataGridView.Rows.Clear();
                            //DataGirdShow((byte)i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                            //Points_LV.Text += (i + 1) + ",R/G/B, V" + Point_Hi3288[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                            //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                            //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";
                        }
                        //Thread.Sleep(10); //1秒=1000毫秒，
                        //T17.Enabled = true;
                        flag1 = 1;  //复位 亮度色坐标符合目标标志位
                        flag2 = 1;
                        flag3 = 1;
                        JumpGammaAdjustFlag = false;
                    }
                    rstep = (byte)(int.Parse(numStep.Value.ToString())); ;    //重新获取调节步长
                    gstep = (byte)(int.Parse(numStep.Value.ToString())); ;
                    bstep = (byte)(int.Parse(numStep.Value.ToString())); ;
                    numPatternR.Value = Point_Hi3288[GPN - index];     //设定绑点灰阶
                                                                       //numPatternG.Value = Point_Hi3288[GPN - index];
                                                                       //numPatternB.Value = Point_Hi3288[GPN - index];
                    OLED_Show_Click(btnPatternW, null);//写入Pattern参数   ，显示帮点对应灰阶画面
                                                       //ReceivOKFlag = false;
                    Thread.Sleep(10);
                    Msr_CA310(); Msr_CA310();
                    Msr_CA310(); //修复讨厌的灰阶跳变时CA410数据异常的问题
                    if (!STOPAutoGammaFlag)
                        T17.Enabled = true;
                    else return;
                }
                else
                {
                    if ((GPN - index == 100) | (GPN - index == 200))//这里index最大值为绑点数量-1；倒数第2个绑点 以及倒数第3个绑点可以做技术处理
                    {
                        //GammaValue[0, 1] = 0;  //0绑点不调，直接送零
                        //GammaValue[0, 2] = 0;
                        //GammaValue[0, 0] = 0;
                        GammaValue[GPN - index, 0] = (uint)(GammaValue[GPN - index + 1, 0] * 0.65);  //绑点不调，直接赋值算了，如果有过度不均再手动修正即可
                        GammaValue[GPN - index, 1] = (uint)(GammaValue[GPN - index + 1, 1] * 0.65);
                        GammaValue[GPN - index, 2] = (uint)(GammaValue[GPN - index + 1, 2] * 0.65);

                        CheckandCorrectGammaValue();
                        GammaValueFormat_Click(null, null);
                        //显示当前(待)写入的Gamma寄存器值
                        txtGammaValue.Text = GammaValue[GPN - index, 0] + "\r\n" + GammaValue[GPN - index, 1] + "\r\n" + GammaValue[GPN - index, 2] + "\r\n";
                        GammaValueSet_index(GammaValueTeparr);

                        JumpGammaAdjustFlag = true;
                    }
                    //else if (GPN - index == 2) //倒数第2个绑点
                    //{
                    //}
                    else
                    {
                        if ((flag3 == 1) | (flag2 == 1) | (flag1 == 1))   //这个判断的意义可以猜猜看
                        {
                            {   //Gamma --R----
                                if (lumr > D_lums[GPN - index, 0])
                                {
                                    if ((!lastdirupR)) { timeout++; }
                                    if ((!lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                    GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] - rstep;// (byte)(int.Parse(numStep.Value.ToString()));
                                    lastdirupR = true;
                                }
                                else
                                {
                                    if ((lastdirupR)) { timeout++; }
                                    if ((lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                    GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] + rstep;
                                    lastdirupR = false;
                                }
                            }
                            {//Gamma ---G--
                                if (lumg > D_lums[GPN - index, 1])
                                {
                                    if ((!lastdirupG)) { timeout++; }
                                    if ((!lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                    GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] - gstep;
                                    lastdirupG = true;
                                }
                                else
                                {
                                    if ((lastdirupG)) { timeout++; }
                                    if ((lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                    GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] + gstep;
                                    lastdirupG = false;
                                }
                            }
                            {//--Gamma  ---B---
                                if (lumb > D_lums[GPN - index, 2])
                                {
                                    if ((!lastdirupB)) { timeout++; }
                                    if ((!lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                    GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] - bstep;
                                    lastdirupB = true;
                                }
                                else
                                {
                                    if ((lastdirupB)) { timeout++; }
                                    if ((lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                    GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] + bstep;
                                    lastdirupB = false;
                                }
                            }
                            txttip.Text += "rstep:" + rstep + "gstetp:" + gstep + "bstep:" + bstep + "\r\n";
                            txttip.Select(txttip.Text.Length, 0);
                            txttip.ScrollToCaret();
                        }   //如果Gamma Value不在目标范围内，此处修正之
                        CheckandCorrectGammaValue();
                        GammaValueFormat_Click(null, null);
                        //显示当前(待)写入的Gamma寄存器值
                        txtGammaValue.Text = GammaValue[GPN - index, 0] + "\r\n" + GammaValue[GPN - index, 1] + "\r\n" + GammaValue[GPN - index, 2] + "\r\n";
                        if (index == 1) { GammaValueSet(GammaValueTeparr); }
                        else
                        {
                            GammaValueSet_index(GammaValueTeparr);   //GammaValueSet  GammaValueSet_index
                        }
                        //Thread.Sleep(20);
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死                            
                    }
                    if (!STOPAutoGammaFlag)
                    {
                        T17.Enabled = true;
                        //txttip.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "\r\n";
                        //txttip.Select(txttip.Text.Length, 0);
                        //txttip.ScrollToCaret();
                    }
                    else return;
                }  ////Gamma tuning
            }
            TimTest.Stop();//停止计时
        }
        private void btnVTStop_Click(object sender, EventArgs e)
        {
            T7.Enabled = false;
            STOPAutoGammaFlag = true;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            TimTest.Stop();//停止计时
            txttip.ForeColor = Color.Black;
            btnCalculateHi3288.PerformClick();
        }
        private void btnWriteGamma_Click(object sender, EventArgs e)   //检查GammaValue 并且重新转换成指定格式后写入DDIC
        {
            CheckandCorrectGammaValue();   //检查Gamma 是否存在高低灰阶数值错乱 ,并刷新表格数据              
            GammaValueFormat_Click(null, null);  //Gamma 值与实际地址进行转换 
            GammaValueSet(GammaValueTeparr); //GammaValue 转换成特定格式后再通过此函数写入DDIC
            //index = 5;
            //GammaValueSet_index(GammaValueTeparr); //GammaValue 转换成特定格式后再通过此函数写入DDIC
            //index = 5;
            //GammaValueSet_index(GammaValueTeparr); //GammaValue 转换成特定格式后再通过此函数写入DDIC
        }
        uint GammaValueMax = 0xFFF - 1;
        private void CheckandCorrectGammaValue()   //检查GammaValue 是否按照大小排列
        {
            byte i; GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            if (GammaValue[GPN - 1, 0] > GammaValueMax)
            {
                GammaValue[GPN - 1, 0] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 1] > GammaValueMax)
            {
                GammaValue[GPN - 1, 1] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 2] > GammaValueMax)
            {
                GammaValue[GPN - 1, 2] = GammaValueMax - 16;
            }  //确保GammaValue不要超过DDIC 中Gamma寄存能接受的最大值
            //---Gamma15绑点后的值要与15绑点相同
            for (i = (GPN); i < 36; i++)
            {
                GammaValue[i, 0] = GammaValue[GPN - 1, 0];
                GammaValue[i, 1] = GammaValue[GPN - 1, 1];
                GammaValue[i, 2] = GammaValue[GPN - 1, 2];
            }
            //-------------------------------------------------------
            if (chkBandPointCheck.Checked)  //检查绑点是否翻转
            {
                for (i = (byte)(GPN - 1); i > 0; i--)    //低灰阶GammaValue如果大于高灰阶GammaValue则强制变小
                {
                    if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                    {
                        GammaValue[i - 1, 0] = GammaValue[i, 0] - 0;
                        //GammaValueFormat_Click(null, null);
                    }
                    if (GammaValue[i, 1] < GammaValue[i - 1, 1])
                    {
                        GammaValue[i - 1, 1] = GammaValue[i, 1] - 0;
                        //GammaValueFormat_Click(null, null);
                    }
                    if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                    {
                        GammaValue[i - 1, 2] = GammaValue[i, 2] - 0;
                        //GammaValueFormat_Click(null, null);
                    }
                }
            }
            GammaValue[0, 0] = 0; GammaValue[0, 1] = 0; GammaValue[0, 2] = 0;
            if (chkGammaGridViewFresh.Checked)   // 刷新UI界面GammaValue值显示
            {
                this.GammaReg_dataGridView.Rows.Clear();
                for (i = 0; i < GPN; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }
            }
        }
        private void btnGammaValueUpdate_Click(object sender, EventArgs e)      //刷新UI上表格内的Gamma数据
        {
            Gammnum = (byte)numGammaPage.Value;
            int Line_Count = 0;
            try
            {
                Line_Count = GammaReg_dataGridView.RowCount;
            }
            catch
            {
                MessageBox.Show("表格数据(行数)异常！", "提示：");
                return;
            }
            try
            {
                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                {
                    GammaValue[i, 0] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[2].Value);
                    GammaValue[i, 1] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[3].Value);
                    GammaValue[i, 2] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[4].Value);
                }
                //MessageBox.Show("数据更新OK！", "提示：");
                txttip.Text += "Gamma数据更新OK！\r\n";
                btnWriteGamma.PerformClick();
            }
            catch
            {
                MessageBox.Show("Gamma数据更新错误！", "注意：");
            }
        }
        private void numStep_ValueChanged(object sender, EventArgs e)   //手动调整AutoGamma调节步长以在初次调整Gamma是提升速度
        {
            rstep = (byte)(int.Parse(numStep.Value.ToString()));
            gstep = (byte)(int.Parse(numStep.Value.ToString()));
            bstep = (byte)(int.Parse(numStep.Value.ToString()));
        }
        private void chkHi3288dircshow_CheckedChanged(object sender, EventArgs e)     //Gamma保存时辅助选项 1
        {
            //if (chkHi3288dircshow.Checked)
            //    chkGammaFormatForHi3288.Checked = false;
        }
        private void chkGammaFormatForHi3288_CheckedChanged(object sender, EventArgs e)   ////Gamma保存时辅助选项 2
        {
            //  if (chkGammaFormatForHi3288.Checked)
            //    chkHi3288dircshow.Checked = false;
        }
        #region  //Gamma调整计时
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        private void TimTest_Tick(object sender, EventArgs e)
        {
            TimeCount = DateTime.Now - TimeNow;
            txtTimeShow.Text = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
        }
        #endregion
        private byte[] arrbyte_tmp = new byte[250];         //临时保存下位机返回值
        public void DataGirdShow(byte index, double RRR, double GGG, double BBB)        //UI 表格显示 R/G/B对应Gamma寄存器值
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();

            Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.GammaReg_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.GammaReg_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.GammaReg_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.GammaReg_dataGridView.Rows.Add(Row1);
            }
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[0].Value = index + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[1].Value = (D_lum[index]).ToString("#0.0000");
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[2].Value = RRR;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[3].Value = GGG;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[4].Value = BBB;

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
                this.GammaReg_dataGridView.Rows[Current_Index].Selected = false;
                this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            GammaReg_dataGridView.FirstDisplayedScrollingRowIndex = GammaReg_dataGridView.RowCount - 1;
        }
        #endregion  //--- Auto Gamma-----
        #region //通用命令处理
        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        if (rdoHisiCode.Checked)
                        {
                            txt3288Input.Text = txtCmd1.Text;
                            btnCodeConvert_Click(null, null);
                            btnCmdSendX_Click(null, null);
                            return;
                        }
                        else
                        {
                            frmTop.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        }
                    }
                    break;
                case "btnSend2":
                    {
                        if (rdoHisiCode.Checked)
                        {
                            txt3288Input.Text = txtCmd2.Text;
                            btnCodeConvert_Click(null, null);
                            btnCmdSendX_Click(null, null);
                            return;
                        }
                        else
                        {
                            frmTop.txtProcessDrivercode(txtCmd2.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        }
                    }
                    break;
                case "btnSend4":
                    {
                        if (rdoHisiCode.Checked)
                        {
                            txt3288Input.Text = txtCmd4.Text;
                            btnCodeConvert_Click(null, null);
                            btnCmdSendX_Click(null, null);
                            return;
                        }
                        else
                        {
                            frmTop.txtProcessDrivercode(txtCmd4.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        }
                    }
                    break;
                case "btnSend5":
                    {
                        if (rdoHisiCode.Checked)
                        {
                            txt3288Input.Text = txtCmd5.Text;
                            btnCodeConvert_Click(null, null);
                            btnCmdSendX_Click(null, null);
                            return;
                        }
                        else
                        {
                            frmTop.txtProcessDrivercode(txtCmd5.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        }
                    }
                    break;
                case "btnSend6":
                    {
                        if (rdoHisiCode.Checked)
                        {
                            txt3288Input.Text = txtCmd6.Text;
                            btnCodeConvert_Click(null, null);
                            btnCmdSendX_Click(null, null);
                            return;
                        }
                        else
                        {
                            frmTop.txtProcessDrivercode(txtCmd6.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        }
                    }
                    break;
                case "btnSend3":
                    {
                        frmTop.txtProcessDrivercode(txtCmd3.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;

            }
            Setting.TempRecord.cmdLen = 6;
            if (rBt_LP_DCS_Long_Write.Checked | rBt_LP_DCS_Short_Write.Checked | rBt_LP_Gerneric_Long_Write.Checked | rBt_LP_Gerneric_Short_Write.Checked)
            {

                frmTop.SendCmd(0);
            }
            else
            {
                frmTop.SendCmd(4);
            }
            showmsg("命令已下发...\r\n");
        }
        private void SaveCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "//-----保存Driver IC Code文件------\r\n";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                try
                {
                    if (ChkCMD1.Checked)
                    {
                        str += "//---CMD1---\r\n" + txtCmd1.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD2.Checked)
                    {
                        str += "//---CMD2---\r\n" + txtCmd2.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD3.Checked)
                    {
                        str += "//---CMD3---\r\n" + txtCmd3.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Driver IC Code保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Driver IC Code保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        private void OpenTxtCode_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";

            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(of.FileName, Encoding.Default);
                switch (btn.Name)
                {
                    case "OpenTxtCode":
                        {
                            txtCmd1.Clear();
                            txtCmd1.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode2":
                        {
                            txtCmd2.Clear();
                            txtCmd2.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode3":
                        {
                            txtCmd3.Clear();
                            txtCmd3.Text = streamReader.ReadToEnd();
                        }
                        break;

                    default: break;
                }
                streamReader.Close();
            }
        }
        #endregion
        string path = "";

        //------------------------------------------------------------        
        private void Hi3288_Gamma_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
        #region    //浏览图片
        //string path = "";
        private void btnBrowsePic_Click(object sender, EventArgs e)    //载入画面按键  选择文件夹
        {
            chkGammaLoadBMP.Enabled = true;
            if (txtbmp.Text != "")
            {
                frmTop.defaultfilePath = txtbmp.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                frmTop.defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择            
            if (frmTop.defaultfilePath != "")
            {
                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = frmTop.defaultfilePath;
            }

            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            frmTop.defaultfilePath = fldb.SelectedPath;
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);//递归加载文件夹里的所有图片
            txtbmp.Text = path;
            // -------------------------------------------------------------------------
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(frmTop.defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
        }
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }

            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))//找到一个图片(如果用户手贱把一个excel文件后缀改成jpeg没办法)
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            //Image img = new Bitmap(selectedImg.Path);
            //pictureBox1.Image = img;
            FileStream fileStream = new FileStream(selectedImg.Path, FileMode.Open, FileAccess.Read);
            try
            {
                pictureBox1.Image = Image.FromStream(fileStream);
            }
            catch
            {
                pictureBox1.Image = null; txttip.Text += "\r\n" + "-----非标准图片-----\r\n";
            }
            fileStream.Close();
            fileStream.Dispose();
        }
        private byte ircbmpflag = 1;
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            Int32 HACT_True = 0;
            Int32 HACT_Skip = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;
            byte tep;
            Bitmap image1;
            //byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;

            string bmpFilepath = txtbmp.Text + '\\' + numPatternR.Value.ToString() + ".dsc";
            if (chkGammaLoadBMP.Checked)
            {
                aFile = new FileStream(bmpFilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            else
            {
                MyImage FileName = (MyImage)listBox3_BMPFile.SelectedItem;
                bmpFilepath = FileName.Path;
                aFile = new FileStream(bmpFilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }

            ByteReaded = aFile.Read(TemFileContent, 0, 54);
            Int32 BMoffset = ((TemFileContent[13] << 24) + (TemFileContent[12] << 16) + (TemFileContent[11] << 8) + TemFileContent[10]);
            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if ((TemFileContent[0] == 0x44) && (TemFileContent[1] == 0x53) && (TemFileContent[2] == 0x43))
            { //此時dui'ying DSC 压缩图片
                BMoffset = 132;
                HACT_True = ((TemFileContent[12]) << 8) + TemFileContent[13];
                VACT_True = ((TemFileContent[10] << 8) + TemFileContent[11]);
                HACT_True /= 3;
            }

            if (chkAutosize.Checked)
            {
                // FileStream fileStream = new FileStream(selectedImg.Path, FileMode.Open, FileAccess.Read);
                // pictureBox1.Image = Image.FromStream(fileStream);
                image1 = new Bitmap(bmpFilepath, true);
                image1 = KiCut(image1, 0, 0, (int)frmTop.HACT.Value, (int)frmTop.VACT.Value);
                //FileStream bFile;
                //bFile = new FileStream();
                //image1.Save(bFile, System.Drawing.Imaging.ImageFormat.Bmp);
                //bFile.Seek(0, SeekOrigin.Begin);//            aFile.Position = 0;            
                image1.Save(@".\tempW2x.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                pictureBox1.Image = image1;
                aFile = new FileStream(@".\tempW2x.bmp", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                BMoffset = ((TemFileContent[13] << 24) + (TemFileContent[12] << 16) + (TemFileContent[11] << 8) + TemFileContent[10]);
                HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            }
            else if (chkSizeCheck.Checked)
            {
                if (((TemFileContent[0] == 0x44) && (TemFileContent[1] == 0x53) && (TemFileContent[2] == 0x43)) ? (frmTop.HACT.Value != (HACT_True)) : (frmTop.HACT.Value != HACT_True))
                {
                    txttip.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "图片宽度与设定不符，如果是DSC图片可尝试取消分辨率检查\r\n";
                    return;
                }
            }
            if ((chkFastSendBmp.Checked) && ((TemFileContent[0] == 0x42) && (TemFileContent[1] == 0x4d)))     // 2020.1.27 暂时适配了VIDEO/CMD 两种；SPI 未处理
            {
                try
                {   //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了  ------//bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmTop.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 0;
                    int x, y, s = 0;
                    if (frmTop.Dis_mode.SelectedIndex == 1)  //video 模式
                    {
                        x = 512 * 4000; //单次传输数量
                    }
                    else
                    {
                        x = HACT_True * 3; //单次传输数量
                    }
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                    { y++; }

                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            aFile.Seek((j) * x + BMoffset, SeekOrigin.Begin);
                            ByteReaded = aFile.Read(TemFileContent, 0, x);
                            try
                            {
                                frmTop.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label3.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }
                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmTop.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else if (((TemFileContent[0] == 0x44) && (TemFileContent[1] == 0x53) && (TemFileContent[2] == 0x43))) //DSC 图片
            {
                if (HACT_True % 4 != 0)  //若图片宽度不能被4整除则使用实际UI标示的分辨率@C11项目
                {
                    HACT_True = (int)frmTop.HACT.Value; VACT_True = (int)frmTop.VACT.Value;
                }
                if ((frmTop.Dis_mode.SelectedIndex == 1) && (Pixel_Offset_Enable.Checked == true)) //video
                {
                    HACT_Skip = HACT_True % 10 > 0 ? (HACT_True + 10 - HACT_True % 10) : HACT_True;
                }
                else
                {
                    HACT_Skip = HACT_True;
                }
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmTop.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 0;
                    while (j < VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((j) * (HACT_True * 3 + (HACT_True % 4)) + BMoffset, SeekOrigin.Begin);
                        else
                            aFile.Seek((j) * HACT_True * 3 + BMoffset, SeekOrigin.Begin);

                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        for (int m = 0; m < HACT_True * 3; m += 3)
                        {
                            tep = TemFileContent[m];
                            TemFileContent[m] = TemFileContent[m + 2];
                            TemFileContent[m + 2] = tep;   //R/G/B交换
                        }
                        try
                        {
                            frmTop.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label3.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;

                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmTop.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        return;
                    }
                    aFile.Close();
                }
                catch
                {
                    txttip.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "啥图片都没显示";
                    txttip.Select(txttip.Text.Length, 0);
                    txttip.ScrollToCaret();
                }
            }
            else
            {
                if ((frmTop.Dis_mode.SelectedIndex == 1) && (Pixel_Offset_Enable.Checked == true)) //video
                {
                    HACT_Skip = HACT_True % 10 > 0 ? (HACT_True + 10 - HACT_True % 10) : HACT_True;
                }
                else
                {
                    HACT_Skip = HACT_True;
                }
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    TemFileContent[4] = Channel_R.Checked == false ? (byte)(TemFileContent[4] | 0x01) : TemFileContent[4];
                    TemFileContent[4] = Channel_G.Checked == false ? (byte)(TemFileContent[4] | 0x02) : TemFileContent[4];
                    TemFileContent[4] = Channel_B.Checked == false ? (byte)(TemFileContent[4] | 0x04) : TemFileContent[4];
                    frmTop.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    //HACT_True = Sig_mode.SelectedIndex > 2 ? Convert.ToInt32(2 * ((TemFileContent[19]<<8)+ TemFileContent[18])) : Convert.ToInt32((TemFileContent[19] << 8) + TemFileContent[18]);
                    j = 1;
                    while (j < VACT_True)
                    {

                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + BMoffset, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + BMoffset, SeekOrigin.Begin);

                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            frmTop.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label3.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;

                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmTop.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        return;
                    }
                    aFile.Close();
                }
                catch
                {
                    txttip.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "啥图片都没显示";
                    txttip.Select(txttip.Text.Length, 0);
                    txttip.ScrollToCaret();
                }
            }
        }
        public static Bitmap KiCut(Bitmap Obmp, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (Obmp == null)
            {
                return null;
            }
            int w = Obmp.Width; int h = Obmp.Height;
            if (StartX >= w || StartY >= h)
            {
                return null;
            }
            if (StartX + iWidth > w)
            {
                // iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                // iHeight = h - StartY;
            }
            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);

                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(Obmp, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                //bm.Save(@".\vertical_gray_10.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                g.Dispose();

                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }
        //-----------------------------------------
        #endregion
        #region     //-----Demura----
        Int32 DemuraBinSize = 0;
        private void btnData2DRAM_Click(object sender, EventArgs e)
        {
            Int32 File_Length;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            string FileName = txtDemuraAddr.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
            FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            File_Length = Convert.ToInt32(aFile.Length);
            DemuraBinSize = File_Length;
            if (DemuraBinSize % 96 != 0)
            {
                MessageBox.Show("Bin文件不能被96整除，请确认是否选择新文件！");
                byte[] arr96n = new byte[(DemuraBinSize / 96 + 1) * 96];
                FileName += "Resiz96.bin";
                System.IO.FileStream fs = File.OpenWrite(FileName);   //  //输出文件
                aFile.Read(arr96n, 0, File_Length);
                if (arr96n[arr96n.Length - 1] == 0)
                { arr96n[arr96n.Length - 1] = 6; }
                if (arr96n[arr96n.Length - 2] == 0)
                { arr96n[arr96n.Length - 2] = 9; }
                fs.Write(arr96n, 0, arr96n.Length);
                fs.Close(); fs.Dispose();
                showmsg("--新bin文件保存OK!--");
                //return;
            }
            showmsg("DemuraBinSize:" + DemuraBinSize + "Byte");
            aFile.Seek(0 + 0x13f300, SeekOrigin.Begin);    //跳转
            ByteReaded = aFile.Read(TemFileContent, 0, 2); //
            showmsg("CRC:" + String.Format("{0:X2}", TemFileContent[0]) + "," + String.Format("{0:X2}", TemFileContent[1]) + "\r\n");
            showmsg("The transfer is complete" + "\r\n");
            aFile.Close();

            if (frmTop.DemuraBinLoad("00", "" + nummipiPPP.Value, "2C", "3C", FileName) == 1) //packtype 00=下位机不加地址
            //   (string packtype, string packsize, string Addr_first, string Addr_second, string BinFilePath)//packsize 必须是偶数
            {
                showmsg("Bin Load OK");
            }
            else
            {
                showmsg("Bin Load NG");
                return;
            }
        }
        private void btnCleanTextBox_Click(object sender, EventArgs e)
        {
            txttip.Enabled = true;
            txttip.Clear();
        }
        private void btnprogramEnd_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtTestcode2.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }
        private void btnDemuraflashParsSet_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtDemuraflashParsSet.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }
        bool readokflag = false;
        private void Demura_Function_ON_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Demura_Function_ON, "在此右键，即可DemuraOFF");
        }
        private void Demura_Function_ON_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Demura_Function_ON.PerformClick();
            }
            else
            {
                Demura_Function_OFF.PerformClick();
            }
        }
        private void BIN_load_path(object sender, KeyEventArgs e)       //选择bin/hex文件所在目录
        {
            if (e.KeyCode == Keys.Enter)
            {
                string path = txtDemuraAddr.Text;

                if (!Directory.Exists(path))
                {
                    MessageBox.Show("文件夹不存在.");
                    return;
                }
                listBox3_Demura_File.Items.Clear();
                string s = null;
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.*");
                // List<string> lstr = new List<string>();
                foreach (FileInfo file in Files)
                {
                    s = file.Name.ToLower();
                    if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin"))
                    {
                        listBox3_Demura_File.Items.Add(s);
                    }
                }
                txtDemuraAddr.Text = path;
            }
        }
        string defaultfilePath = "";
        private void btnReload_Demura_File_Click(object sender, EventArgs e)    //选择bin/hex文件
        {
            //btnOneKey.Enabled = true;
            if (txtDemuraAddr.Text != "")
            {
                defaultfilePath = txtDemuraAddr.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;

            if (defaultfilePath != "")
            {                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = defaultfilePath;
            }

            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件.");
                return;
            }
            string path = fldb.SelectedPath;

            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(path);
            //bin文件 识别
            FileInfo[] Files = d.GetFiles(("*.*"));
            foreach (FileInfo file in Files)
            {
                s = file.Name.ToLower();
                if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin"))
                {
                    listBox3_Demura_File.Items.Add(s);
                }
            }
            btnData2DRAM.Enabled = true;
            txtDemuraAddr.Text = path;
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
        }

        private void Demura_Function_ON_Click(object sender, EventArgs e)
        {
            DDIC_Demura_Enble(true);
        }

        private void Demura_Function_OFF_Click(object sender, EventArgs e)
        {
            DDIC_Demura_Enble(false);
        }

        #endregion  //Demura
        #region  //----------------------

        private void PMU_OPERATION_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[38];
            Setting.TempRecord.cmdLen = 38; //33+5
            Send_buf[0] = 0x9E;
            Send_buf[1] = 0x05; //lock
            Send_buf[2] = 33; //length
            Send_buf[4] = 0x00;

            switch (btnTmp.Name)
            {
                case "btnPowertest":
                    {
                        Send_buf[3] = 0x00; //oled功耗测量
                    }
                    break;

            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[36], out Send_buf[37]);
            if (!frmTop.comPort.IsOpen)
            {
                try
                {
                    frmTop.comPort.PortName = Detection.GetComNum();
                    frmTop.comPort.BaudRate = Setting.ComSet.comBaudRate;
                    frmTop.openSerialPort(true);
                }
                catch
                {
                    MessageBox.Show("没有搜索到串口！", "错误提示");
                }
            }
            {
                try
                {
                    frmTop.comPort.Write(Send_buf, 0, 38);
                }
                catch
                {
                    //Tim_Powertest.Enabled = false;
                    //chkAutoPowertest.Checked = false;
                    MessageBox.Show("串口有误，请检查", "错误");
                }
                frmTop.T1.Enabled = false;
                frmTop.T1.Interval = 2000;
                frmTop.T1.Enabled = true;
            }
        }

        private void Hi3288_GammaR_Click(object sender, EventArgs e)
        {

        }

        private void chkAODMode_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkAODMode.Checked)
            {
                regSend.Text = "38";
                writeReg.PerformClick();
                //    rbt_Hi3288_Gamma1.Enabled = true; rbt_Hi3288_Gamma2.Enabled = true; rbt_Hi3288_Gamma3.Enabled = true; rbt_Hi3288_Gamma4.Enabled = true; rbt_Hi3288_Gamma5.Enabled = true; rbt_Hi3288_Gamma6.Enabled = true; rbt_Hi3288_Gamma7.Enabled = true; rbt_Hi3288_Gamma8.Enabled = true; rbt_Hi3288_Gamma9.Enabled = true; rbt_Hi3288_Gamma10.Enabled = true;
            }
            else
            {
                numGammaPage.Value = 0x1E;
                regSend.Text = "39";
                writeReg.PerformClick();
                regSend.Text = "51,07,FF";
                MessageBox.Show("请确认当前51值设定是否正确", "提示");
                btnAutoGammas.Enabled = true;
                // rbt_Hi3288_Gamma1.Enabled = false; rbt_Hi3288_Gamma2.Enabled = false; rbt_Hi3288_Gamma3.Enabled = false; rbt_Hi3288_Gamma4.Enabled = false; rbt_Hi3288_Gamma5.Enabled = false; rbt_Hi3288_Gamma6.Enabled = false; rbt_Hi3288_Gamma7.Enabled = false; rbt_Hi3288_Gamma8.Enabled = false; rbt_Hi3288_Gamma9.Enabled = false; rbt_Hi3288_Gamma10.Enabled = false;
            }
        }

        private void chkBISTPattern_CheckedChanged(object sender, EventArgs e)
        {
            txttip.Text = "勾选后可显示BIST 内置测试图";

            if (chkBISTPattern.Checked)
            {
                numPatternR.Value = 5;
                numPatternR.Maximum = 17;
                btnPatternW.PerformClick();
            }
            else
            {
                numPatternR.Maximum = 255;
                numPatternR.Value = 64;
            }
        }
        string tiptxtFlag = "";  //保存返回值提示信息
        private void btnReadDemura_FlashID_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtReadFlashID.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
            Thread.Sleep(50);
            tiptxtFlag = "FlashID:";
            frmTop.GeneralRegRead(0xc0, 4);
        }

        private void Hi3288_Load(object sender, EventArgs e)
        {
            //tabPageIRC.Parent = null;
        }

        Single[,] Gammadbv2Arr = new Single[1, 1];
        byte TotalGammaNum;
        private void txt51Dbv_TextChanged(object sender, EventArgs e)
        {
            string Gammadbv;
            string[] GammadbvGroup = new string[5];
            TotalGammaNum = (byte)txt51Dbv.Lines.Length;
            for (int i = 0; i < TotalGammaNum; i++)
            {
                if (txt51Dbv.Lines[i] == "")
                {
                    TotalGammaNum = (byte)i;
                    break;
                }
            }
            GammadbvGroup = new string[TotalGammaNum];
            Gammadbv2Arr = new Single[TotalGammaNum, 3];
            for (int i = 0; i < TotalGammaNum; i++)
            {
                // GammadbvGroup[i] = txt51Dbv.Lines[i];
                Gammadbv = txt51Dbv.Lines[i]; //GammadbvGroup[i];
                Gammadbv = Gammadbv.Replace(" ", ""); //去空格
                Gammadbv = Gammadbv.Replace(",", " "); // “;”换成空格
                GammadbvGroup = Gammadbv.Trim().Split(' ');
                if (GammadbvGroup[0] == "")
                { break; }
                else
                {
                    try
                    {
                        //byte.Parse((Convert.ToInt32(strTmp, 16)).ToString());  
                        Gammadbv2Arr[i, 0] = (Convert.ToInt32(GammadbvGroup[0], 16));
                        Gammadbv2Arr[i, 1] = (Convert.ToSingle(GammadbvGroup[1])); //  Convert.float(GammadbvGroup[1]);
                        Gammadbv2Arr[i, 2] = (Convert.ToSingle(GammadbvGroup[2]));
                    }
                    catch (FormatException ee)
                    { Console.WriteLine(ee.Message); }
                }
            }
            txttip.Text += (TotalGammaNum) + "条Gamma待调试";
        }

        bool GammatuningOKFlag = false;
        uint[,,] GammaValx = new uint[26, 36, 3];  //提前预定义GammaValue存储区，防止被意外覆盖
                                                   //预定义26条Gamma，25个绑点，R/G/B3个值
                                                   //GammaValx[0]
        private void GammavalMove(int movedir, int n, uint[,,] a_GroupGammaVal, uint[,] b_GammaVal)
        {
            if (movedir == 0)
            {
                for (int inx = 0; inx < 36; inx++)
                {
                    b_GammaVal[inx, 0] = a_GroupGammaVal[n, inx, 0];
                    b_GammaVal[inx, 1] = a_GroupGammaVal[n, inx, 1];
                    b_GammaVal[inx, 2] = a_GroupGammaVal[n, inx, 2];
                }
            }
            else
            {
                for (int inx = 0; inx < 36; inx++)
                {
                    a_GroupGammaVal[n, inx, 0] = b_GammaVal[inx, 0];
                    a_GroupGammaVal[n, inx, 1] = b_GammaVal[inx, 1];
                    a_GroupGammaVal[n, inx, 2] = b_GammaVal[inx, 2];
                }
            }
        }

        private void btnGroupGammaTune_Click(object sender, EventArgs e)
        {
            GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            //gm0 = GroGv.GammaValue0;
            txt51Dbv_TextChanged(null, null);

            for (int t = 0; t < TotalGammaNum; t++)
            {
                //GammaValue = GroGv.GammaValue0;
                GammatuningOKFlag = false;
                gammaindex = (byte)t;
                txtVT_lum.Text = Gammadbv2Arr[t, 2] + "";
                numGammaPage.Value = (decimal)Gammadbv2Arr[t, 1];

                GammavalMove(0, t, GammaValx, GammaValue); //多组GammaValue保存缓冲区搬出指定低t条Gamma至GammaValue

                btnCalGammaValue_Click(null, null);
                btnGammaValueUpdate.PerformClick();
                //btnGammaValueUpdate_Click(null,null);

                Dbv51setting((int)Gammadbv2Arr[t, 0]);  //设定51

                btnAutoGammas.PerformClick();
                //while (!GammatuningOKFlag) Application.DoEvents();//重点，必须加上，否则父子窗体都假死;
                if (chkSaveGammavalue.Checked)
                {
                    //btnSaveGamma.PerformClick();
                    btnSaveGamma_Click(btnSaveGamma_Auto, null);
                    //Application.DoEvents();//重点，必须加上，否则父子窗体都假死;
                }

                GammavalMove(1, t, GammaValx, GammaValue); //更新多组GammaValue保存缓冲区的第t条Gamma
                txttip.Text += "第" + (t + 1) + "条Gamma调试完成";
                Console.WriteLine("第" + (t + 1) + "条Gamma调试完成");
                //GroGv.GammaValue0=GammaValue;
                //GammV2GammaS((byte)t);
            }
            txttip.Text += "\r\n---多条Gamma调试完成---\r\n";
        }
        private void Dbv51setting(int value)
        {
            byte v51h, v51l; int T51Value;
            num51Value.Value = value;
            T51Value = value * 4;
            T51Value += 0x8000;//MSB为51 使能位
            v51h = Convert.ToByte(T51Value >> 8);
            v51l = Convert.ToByte(T51Value & 0xff);
            regSend.Text = "51," + T51Value.ToString("x4").Substring(2, 2) + "," + T51Value.ToString("x4").Substring(0, 2);
            byte[] tepArr = new byte[] {
            4,255,254,253,0x29,
            5,0xff,0,0,0,0,
            3,0x51,v51l,v51h
            };
            frmTop.comPort.Write(GetCRCArray(tepArr, 4), 0, tepArr.Length + 6);//循环部分，每段数据结尾  
            Thread.Sleep(20);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死            
        }
        private void exitUCSFF0000()
        {
            byte[] tepArr = new byte[] {
            4,255,254,253,0x29,
            5,0xff,0,0,0,0,
            };
            frmTop.comPort.Write(GetCRCArray(tepArr, 4), 0, tepArr.Length + 6);//循环部分，每段数据结尾  
            Thread.Sleep(20);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死            
        }


        string[] nodeGray = new string[36];
        //private byte ActualUseNodeNum;
        private void txtGammaNode_TextChanged(object sender, EventArgs e)
        {
            byte ActualUseNodeNum;
            string Graylv;
            Graylv = txtGammaNode.Text;
            Graylv = Graylv.Replace(" ", ""); //去空格
            Graylv = Graylv.Replace(",", " "); // “;”换成空格
            nodeGray = Graylv.Trim().Split(' ');
            // R66462_Point
            ActualUseNodeNum = (byte)nodeGray.Length;
            labGammaPoint.Text = "当前绑点数量：" + ActualUseNodeNum;
            Point_Hi3288 = new int[ActualUseNodeNum];
            try
            {
                for (byte i = 0; i < ActualUseNodeNum; i++)
                {
                    Point_Hi3288[i] = Convert.ToByte(nodeGray[i]);
                }
                GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            }
            catch { }
        }
        string[] Regdata = new string[2];
        private void btnCodeConvert_Click(object sender, EventArgs e)
        {
            String[] lines = txt3288Input.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            //string speedflag = lines[1];
            String arr, a = "";
            string str1 = "", str;
            uint HiReg, HiData;
            bool precessFlag = false;
            txt3288Output.Text = "";
            byte SecondByte_Add;
            for (int tt = 0; tt < lines.Length; tt++)
            {
                a = lines[tt];
                a = a.Replace(" ", ""); //去空格
                a = a.Replace("DDIC_WRITE_REGISTER", ""); //移除特定字符串
                a = a.Replace("DDIC_Write_Register", ""); //移除特定字符串
                a = a.Replace("(", ""); //移除特定字符串
                a = a.Replace(")", ""); //移除特定字符串
                a = a.Replace(";", ""); //移除特定字符串
                a = a.Replace(".", ","); //
                a = a.Replace("=", ","); //
                a = a.Replace("-", ","); //                
                a = a.Trim(); //从当前字符串删除所有前导空白字符和尾随空白字符。                
                arr = a;
                {
                    int tep = arr.IndexOf("/"); //找出 "/"的具体位置
                    switch (tep)
                    {
                        case 0:     //
                            {//首字母为 "/"
                             //arr 保持不变
                                precessFlag = false;
                            }
                            break;
                        case (-1): // (-1):  不含有"//"
                            {//清理无用的字符
                                arr = Regex.Replace(arr, "[^0-9a-fA-F,xX\r\n]", "");//删除非法字符
                                arr = arr.Trim().TrimEnd(".,/;=".ToCharArray());      //去除行尾多余的“.,/;=”                               
                                precessFlag = true;
                            }
                            break;
                        default: // (>0): 在中间含有"//"，丢弃"/"以后的内容
                            {
                                arr = arr.Substring(0, tep); //(arr2.Length-tep)                            
                                arr = Regex.Replace(arr, "[^0-9a-fA-F,xX\r\n]", "");//删除非法字符
                                arr = arr.Trim().TrimEnd(".,/;=".ToCharArray());      //去除行尾多余的“.,/;=”
                                precessFlag = true;
                            }
                            break;
                    }
                }
                if (!precessFlag)
                {
                    str1 += arr + "\r\n";
                }
                else
                {
                    try
                    {
                        Regdata = arr.Trim().Split(',');  //字符串拆分成数组
                        HiReg = Convert.ToUInt32(Regdata[0], 16);
                        HiData = Convert.ToUInt32(Regdata[1], 16);
                        //txt3288Output.Text += HiReg + "----" + HiData + "\r\n";
                        SecondByte_Add = (byte)((HiReg >> 8) & 0xFF);
                        if ((SecondByte_Add == 0x2C) || (SecondByte_Add == 0x3C))
                        {
                            /*
                                FF,FE,29,FF,02,00,88,29,2C,1F,00,00,00
                                Change to:
                                FF,FE,FD,29
                                FF,02,00,88
                                2C,1F,00,00,00                            */
                            str = String.Format("FF,FE,FD,29\r\nFF,{0:X2},{1:X2},{2:X2}\r\n", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                            str += String.Format("{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                        }
                        else
                        {
                            str = String.Format("FF,FE,29,FF,{0:X2},{1:X2},{2:X2},", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                            str += String.Format("39,{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                        }
                        str1 += str + "\r\n";
                    }
                    catch
                    {
                        txt3288Output.Text = "转换出错，检查输入文本";
                        break;
                    }
                }
            }
            txt3288Output.Text = str1;
        }
        void Delay(int ms)
        {
            Thread.Sleep(ms); // 延时,单位是ms
        }
        //打开关闭Demura
        void DDIC_Demura_Enble(bool EnbleFlag)
        {
            if (EnbleFlag)
            {
                //Opne Demura
                DDIC_Write_Register(0X0700A800, 0X2011486F);
            }
            else
            {
                //Close Demura
                //DDIC_Write_Register(0X0700A800, 0x0);
                //0X2011486E
                DDIC_Write_Register(0X0700A800, 0X2011486E);
            }
            return;
        }
        uint readValue = 0;
        private uint DDIC_Read_Register(uint addr, byte Readlenth)
        {
            uint HiReg;
            byte SecondByte_Add;
            string str; string tepstr;
            try
            {
                HiReg = addr;
                SecondByte_Add = (byte)((HiReg >> 8) & 0xFF);
                {
                    str = String.Format("FF,FE,FD,29\r\nFF,{0:X2},{1:X2},{2:X2}\r\n", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                }
            }
            catch
            {
                txt3288Output.Text = "转换出错，检查输入文本";
                return (0x5a5aFEFF);
            }
            tepstr = txtCmd3.Text;
            txtCmd3.Text = str;
            Send_Cmd_Click(btnSend3, null);
            txtCmd3.Text = tepstr;
            //Thread.Sleep(20);
            readokflag = false;  //重置接收数据完成标志位			
            readValue = 0;
            frmTop.GeneralRegRead(SecondByte_Add, Readlenth);
            while (!readokflag) { Application.DoEvents(); }   //等待接收完成标志；Application.DoEvents()：避免进程卡死			
            return readValue;
        }
        private void DDIC_Write_Register(uint addr, uint para)
        {
            uint HiReg, HiData;
            byte SecondByte_Add;
            string str; string tepstr = "";
            try
            {
                HiReg = addr;   //Convert.ToUInt32(Regdata[0], 16);
                HiData = para;  // Convert.ToUInt32(Regdata[1], 16);
                SecondByte_Add = (byte)((HiReg >> 8) & 0xFF);
                if ((SecondByte_Add == 0x2C) || (SecondByte_Add == 0x3C))
                {
                    /*
                        FF,FE,29,FF,02,00,88,29,2C,1F,00,00,00
                        Change to:
                        FF,FE,FD,29
                        FF,02,00,88
                        2C,1F,00,00,00                            */
                    str = String.Format("FF,FE,FD,29\r\nFF,{0:X2},{1:X2},{2:X2}\r\n", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                    str += String.Format("{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                }
                else
                {
                    str = String.Format("FF,FE,29,FF,{0:X2},{1:X2},{2:X2},", ((HiReg >> 24) & 0xFF), ((HiReg >> 16) & 0xFF), ((HiReg >> 0) & 0xFF));
                    str += String.Format("39,{0:X2},{1:X2},{2:X2},{3:X2},{4:X2}", SecondByte_Add, (byte)(HiData & 0xFF), (byte)((HiData >> 8) & 0xFF), (byte)((HiData >> 16) & 0xFF), (byte)((HiData >> 24) & 0xFF));
                }
            }
            catch
            {
                txt3288Output.Text = "转换出错，检查输入文本";
                return;
            }
            tepstr = txtCmd3.Text;
            txtCmd3.Text = str;
            Send_Cmd_Click(btnSend3, null);
            txtCmd3.Text = tepstr;
            //Thread.Sleep(20);
        }
        private void btnErase_Click(object sender, EventArgs e)
        {
            DDIC_Flash_Init();
            FlashErase();
            showmsg("Flash Erase Done");
        }
        void DDIC_Flash_Init()
        {
            //step1  sf_init
            DDIC_Write_Register(0x01000010, 0x40101);
            DDIC_Write_Register(0x01000010, 0x40001);
            DDIC_Write_Register(0x0100003c, 0x1);
            DDIC_Write_Register(0x02005000, 0x1);
            DDIC_Write_Register(0x0200501c, 0x0);
            DDIC_Write_Register(0x02005020, 0x1ff);
            DDIC_Write_Register(0x02005048, 0xf);

            //step2 WREN  --disable-write-protect.
            DDIC_Write_Register(0x02005020, 0xff);
            DDIC_Write_Register(0x02005030, 0x0);
            DDIC_Write_Register(0x02005024, 0x06);
            DDIC_Write_Register(0x0200503c, 0x81);
            Thread.Sleep(100); //sleep 100 ms.
        }
        //读取FLASH-ID
        uint DDIC_Read_FlashID()
        {
            uint FlashID = 0;
            DDIC_Flash_Init();
            DDIC_Write_Register(0x01000010, 0x3);
            DDIC_Write_Register(0x0100003c, 0x1);
            DDIC_Write_Register(0x02005000, 0x1);
            DDIC_Write_Register(0x0200501c, 0x0);
            DDIC_Write_Register(0x02005020, 0x1ff);
            DDIC_Write_Register(0x02005048, 0xf);
            DDIC_Write_Register(0x02005030, 0x12);
            //read_id_command: 0x9f.
            DDIC_Write_Register(0x02005024, 0x9f);
            DDIC_Write_Register(0x02005038, 0x3);
            DDIC_Write_Register(0x0200503c, 0x85);
            //读0xc000000的低24bit就是flash的id
            FlashID = DDIC_Read_Register(0xc000000, 4);
            FlashID = FlashID & 0XFFFFFF;
            showmsg("FlashID is 0X08X:\n" + FlashID.ToString("X2"));
            return FlashID;
        }
        uint FlashErase()
        {
            //step2 WREN_CMD
            DDIC_Write_Register(0x02005020, 0xff);
            DDIC_Write_Register(0x02005030, 0x0);
            DDIC_Write_Register(0x02005024, 0x06);
            DDIC_Write_Register(0x0200503c, 0x81);
            // delay 100ms
            Thread.Sleep(100);
            //step3 sf_chip_erase
            DDIC_Write_Register(0x02005020, 0xff);
            DDIC_Write_Register(0x02005024, 0xc7);
            DDIC_Write_Register(0x02005030, 0x0);
            DDIC_Write_Register(0x0200503c, 0xC1);
            //读状态(循环下发24~3c配置，读取0xc000000的值，直到bit0=0跳出循环进行后续操作)
            showmsg("Delayms several second\n");
            Thread.Sleep(1000);
            uint readValue = 1;
            uint count = 0;
            while ((readValue & 0x1) != 0)
            {
                DDIC_Write_Register(0x02005024, 0x5);      //读flash状态寄存器低8bit
                DDIC_Write_Register(0x02005030, 0x00);
                DDIC_Write_Register(0x02005038, 0x1);
                DDIC_Write_Register(0x0200503c, 0x85);
                //delay 1ms
                Thread.Sleep(100);
                readValue = DDIC_Read_Register(0x0c000000, 4);
                showmsg("Read 0x0c000000 reg value : 0x%x" + readValue.ToString("X2"));
                count = count + 1;
                if (count > 10)
                {
                    showmsg("Error: Time out\n");
                    return 255;
                }
            }
            showmsg("DDIC_EraseFlash ok\n");
            return 0;
        }
        uint DemuraFlashWrite_bak20220919()
        {
            uint count;
            DDIC_Flash_Init();
            Delay(100);

            //if (flashID == 0)  //GD25xxx IC
            {
                //Console.WriteLine("GD25xxx IC\n");
                //showmsg("GD25xxx IC\n");
                //DDIC_Write_Register(0x02005020, 0xff);
                //DDIC_Write_Register(0x02005030, 0x0);
                //DDIC_Write_Register(0x02005024, 0x01);
                //DDIC_Write_Register(0x02005004, 0x80);
                //DDIC_Write_Register(0x02005038, 0x2);
                //DDIC_Write_Register(0x0c000000, 0x200);
                //DDIC_Write_Register(0x0200503c, 0xa1);
            }
            //else if (flashID == 1)  //芯天下 IC
            //{
            Console.WriteLine("芯天下 IC\n");
            DDIC_Write_Register(0x02005020, 0xff);
            DDIC_Write_Register(0x02005030, 0x0);
            DDIC_Write_Register(0x02005024, 0x31);
            DDIC_Write_Register(0x02005004, 0x80);
            DDIC_Write_Register(0x02005038, 0x1);
            DDIC_Write_Register(0x0c000000, 0x2);
            DDIC_Write_Register(0x0200503c, 0xa1);
            //}
            Delay(100);
            Delay(100);
            //step5 sf_write
            DDIC_Write_Register(0x02005020, 0xff);
            //delay 100ms
            Delay(100);
            DDIC_Write_Register(0x0200502c, 0x0);
            DDIC_Write_Register(0x02005030, 0x1B0);

            DDIC_Write_Register(0x02005040, (uint)DemuraBinSize);
            DDIC_Write_Register(0x0200504c, 0x0B000000);
            DDIC_Write_Register(0x02005068, 0x3203);
            Delay(1000);
            Console.WriteLine(DemuraBinSize + "芯天下 IC\n");
            //重复读0x2005068的值，直到bit0=1
            readValue = DDIC_Read_Register(0x2005068, 4); readValue = DDIC_Read_Register(0x2005068, 4); readValue = DDIC_Read_Register(0x2005068, 4);
            Console.WriteLine("Read 0x2005068 reg value : {0:x}", readValue);
            count = 0;
            while ((readValue & 0x1) == 0)
            {
                readValue = DDIC_Read_Register(0x2005068, 4);
                Console.WriteLine("Read 0x2005068 reg value : {0:x}", readValue);
                Delay(500);
                if ((count++) > 10)
                {
                    Console.WriteLine(" 重复读0x2005068的值 Error: Time out\n");
                    return 255;
                }
            }
            Console.WriteLine("Flash Write Done");
            return 0;
        }
        uint DemuraFlashWrite()
        {
            DDIC_Flash_Init();
            //Now to test SPI-FLASHxxx IC burn spi-flash.
            uint flashID = 1;
            if (flashID == 0)
            {  //GD25xxx IC
                showmsg("GD25xxx IC\n");
                DDIC_Write_Register(0x02005020, 0xff);
                DDIC_Write_Register(0x02005030, 0x0);
                DDIC_Write_Register(0x02005024, 0x01);
                DDIC_Write_Register(0x02005004, 0x80);
                DDIC_Write_Register(0x02005038, 0x2);
                DDIC_Write_Register(0x0c000000, 0x200);
                DDIC_Write_Register(0x0200503c, 0xa1);
            }
            if (flashID == 1)
            { //芯天下 IC
                showmsg("XTX IC\n");
                DDIC_Write_Register(0x02005020, 0xff);
                DDIC_Write_Register(0x02005030, 0x0);
                DDIC_Write_Register(0x02005024, 0x31);
                DDIC_Write_Register(0x02005004, 0x80);
                DDIC_Write_Register(0x02005038, 0x1);
                DDIC_Write_Register(0x0c000000, 0x2);
                DDIC_Write_Register(0x0200503c, 0xa1);
            }
            //delay 100ms
            Thread.Sleep(100);
            //step5 sf_write
            DDIC_Write_Register(0x02005020, 0xff);
            //delay 100ms
            Thread.Sleep(100);
            DDIC_Write_Register(0x0200502c, 0x0);
            DDIC_Write_Register(0x02005030, 0x1b0);

            showmsg("Write Demura Offset:0x3000, Datasize: full Flash.bin Bytes");
            DDIC_Write_Register(0x02005380, 0x3000);         //Demurabin Offset: 0x3000 Bytes.-->12KB
            DDIC_Write_Register(0x0200504c, 0xB003000);   //skip the head of 0x3000 Bytes.
            DDIC_Write_Register(0x02005068, 0x3202);
            DDIC_Write_Register(0x02005394, 0x18800005);

            uint UnitNum = ((uint)DemuraBinSize - 0x3000);
            if ((UnitNum % (12 * 1024)) == 0)
            {
                UnitNum = UnitNum / (12 * 1024);
            }
            else
            {
                UnitNum = UnitNum / (12 * 1024) + 1;
            }
            showmsg("DemuraDataByteNum:%d, Write 0x02005390 reg value : 0x%x" + DemuraBinSize.ToString("x2") + UnitNum.ToString("x2"));
            //0x02005390 dec2hex(UnitNum-1) 
            DDIC_Write_Register(0x02005390, (UnitNum - 1));
            DDIC_Write_Register(0x050000c0, 0x1);
            Thread.Sleep(500);

            //重复读0x2005068的值，直到bit0=0
            uint readValue0 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            uint readValue1 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            uint readValue2 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            showmsg("Read 0x2005068 reg value : 0x%x" + readValue2);
            uint count = 0;
            while (((readValue0 & 0x1) == 1) || ((readValue1 & 0x1) == 1) || ((readValue2 & 0x1) == 1))
            {  //0:结束退出；1：繁忙；
                readValue0 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                readValue1 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                readValue2 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                showmsg("Read 0x2005068 reg value : 0x%x" + readValue2);

                count = count + 1;
                if (count > 10)
                {
                    showmsg("Error: Time out\n");
                    return 255;
                }
                Thread.Sleep(300);
            }
            showmsg("F_Burn_NOGammaData_OtherFlashBin ok\n");
            return 0;
        }
        private void btnDemurastartProgram_Click(object sender, EventArgs e)
        {
            DemuraFlashWrite();
            showmsg("Demura Flash Write Done");
        }
        uint F_Burn_GammaData_FlashBin()          //Gamma bin Write Flow
        {
            DDIC_Flash_Init();
            //Now to test SPI-FLASHxxx IC burn spi-flash.
            uint flashID = 1;
            if (flashID == 0)
            {  //GD25xxx IC
                showmsg("GD25xxx IC\n");
                DDIC_Write_Register(0x02005020, 0xff);
                DDIC_Write_Register(0x02005030, 0x0);
                DDIC_Write_Register(0x02005024, 0x01);
                DDIC_Write_Register(0x02005004, 0x80);
                DDIC_Write_Register(0x02005038, 0x2);
                DDIC_Write_Register(0x0c000000, 0x200);
                DDIC_Write_Register(0x0200503c, 0xa1);
            }
            if (flashID == 1)
            {  //芯天下 IC
                showmsg("XTX IC\n");
                DDIC_Write_Register(0x02005020, 0xff);
                DDIC_Write_Register(0x02005030, 0x0);
                DDIC_Write_Register(0x02005024, 0x31);
                DDIC_Write_Register(0x02005004, 0x80);
                DDIC_Write_Register(0x02005038, 0x1);
                DDIC_Write_Register(0x0c000000, 0x2);
                DDIC_Write_Register(0x0200503c, 0xa1);
            }
            //delay 100ms
            Thread.Sleep(100);
            //step5 sf_write
            DDIC_Write_Register(0x02005020, 0xff);
            //delay 100ms
            Thread.Sleep(100);
            DDIC_Write_Register(0x0200502c, 0x0);
            DDIC_Write_Register(0x02005030, 0x1b0);

            showmsg("Write BCMG_LUT Offset:0x00, Datasize: 0x3000 Bytes");
            DDIC_Write_Register(0x02005380, 0x00);     //Offset: 0x00 Bytes-->for Gamma Flash
            DDIC_Write_Register(0x0200504c, 0x0B000000);//Gamma数据在RAM内的地址
            DDIC_Write_Register(0x02005068, 0x3202);
            DDIC_Write_Register(0x02005394, 0x18800005);
            DDIC_Write_Register(0x02005390, 0x0); //烧写长度为(0+1)*12KB;
            DDIC_Write_Register(0x050000c0, 0x1);   //使能
            Thread.Sleep(100);
            //重复读0x2005068的值，直到bit0=0
            uint readValue0 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            uint readValue1 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            uint readValue2 = DDIC_Read_Register(0x2005068, 4);
            Thread.Sleep(10);
            showmsg("Read 0x2005068 reg value : 0x%x" + readValue2);
            uint count = 0;
            while (((readValue0 & 0x1) == 1) || ((readValue1 & 0x1) == 1) || ((readValue2 & 0x1) == 1))
            {  //0:结束退出；1：繁忙；
                readValue0 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                readValue1 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                readValue2 = DDIC_Read_Register(0x2005068, 4);
                Thread.Sleep(10);
                showmsg("Read 0x2005068 reg value : 0x%x" + readValue2);

                count = count + 1;
                if (count > 10)
                {
                    showmsg("Error: Time out\n");
                    return 255;
                }
                Thread.Sleep(300);
            }
            showmsg("F_Burn_GammaData_FlashBin ok\n");
            return 0;
        }

        private void btnGammaBinWrite_Click(object sender, EventArgs e)
        {
            F_Burn_GammaData_FlashBin();
            showmsg("Gamma Flash Write Done");
        }
        bool debugprint = false;
        private void showmsg(string str, params byte[] mesgshowLevel)
        {
            byte MsgshowLevel;
            if (mesgshowLevel != null && mesgshowLevel.Length > 0)
            {
                MsgshowLevel = mesgshowLevel[0];
            }
            else { MsgshowLevel = 0; }
            if ((MsgshowLevel == 0) || ((MsgshowLevel > 0) && (debugprint)))
            {
                txttip.Text += str + "\r\n";
                txttip.SelectionStart = this.txttip.Text.Length;
                txttip.ScrollToCaret();//滚动到光标处
                if (txttip.Lines.Length > 5000) txttip.Clear();
            }
        }
        private void btn51Write_Click(object sender, EventArgs e)
        {
            regSend.Text = "";
            uint tep51Value = (uint)num51Value.Value * 4;
            tep51Value += 0x8000;  //MSB为51 使能位
            string str = "51";
            {
                str += "," + tep51Value.ToString("x4").Substring(2, 2) + "," + tep51Value.ToString("x4").Substring(0, 2);
            }
            regSend.Text = str;
            //writeReg.PerformClick();
            GR_WriteRead_Click(writeReg, null);
        }

        private void chkMesgshwoLevel_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMesgshwoLevel.Checked) { debugprint = true; }
            else { debugprint = false; }
        }

        private void btnCmdSendX_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txt3288Output.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
            Thread.Sleep(50);
            showmsg("代码下发完成");
            //tiptxtFlag = "FlashID:";
            //frmTop.GeneralRegRead(0xc0, 4);
        }

        private void rdoHisiCode_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoHisiCode.Checked)
            {
                txtCmd3.Enabled = false;
                btnSend3.Enabled = false;
            }
            else
            {
                {
                    txtCmd3.Enabled = true;
                    btnSend3.Enabled = true;
                }
            }
        }
        private void btnPrepareForDemuraBin_Click(object sender, EventArgs e)
        {
            DDIC_Write_Register(0x02006034, 0x00221100);
            DDIC_Write_Register(0x05000000, 0x02030003);
            DDIC_Write_Register(0x0500000c, 0x00000000);
            DDIC_Write_Register(0x05000040, 0x00010000);
            DDIC_Write_Register(0x05000014, 0x00000000);
            DDIC_Write_Register(0X05000018, 0X00000060);
            DDIC_Write_Register(0x05001000, 0x00000000);
            DDIC_Write_Register(0x05002000, 0x00000000);
            DDIC_Write_Register(0x0600c044, 0x40000000);
            showmsg("屏幕变黑");
        }

        private void numGammaPage_ValueChanged(object sender, EventArgs e)
        {
            Gammnum = (byte)numGammaPage.Value;
        }

        private uint[,] GammaValue0 = new uint[36, 3];//保存  Normal  Gammavalue
        private uint[,] GammaValue1 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue2 = new uint[36, 3];//保存当前Gammavalue

        private void btnGammaPrepare_Click(object sender, EventArgs e)
        {
            txtGammaNode_TextChanged(null, null);  //获取Gamma绑点数据   //GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量        
            InitGammaValue();
            showmsg("Wait...");
            GammaWritePrepre();
            showmsg("完成GammaValue 写入准备");
            btnAutoGammas.Enabled = true;
        }
        private void btnGammaTest_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtGammaTest.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private uint[,] GammaValue3 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue4 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue5 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue6 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue7 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue8 = new uint[36, 3];//保存当前Gammavalue
        private uint[,] GammaValue9 = new uint[36, 3];//保存当前Gammavalue

        private void InitGammaValue()
        {
            uint GValue = 3080;
            for (int i = 0; i < 36; i++)
            {
                if (i >= 15)
                {
                    GValue = 2000;
                    GammaValue[i, 0] = GValue;
                    GammaValue[i, 1] = GValue;
                    GammaValue[i, 2] = GValue;

                    GammaValue0[i, 0] = GValue;
                    GammaValue0[i, 1] = GValue;
                    GammaValue0[i, 2] = GValue;

                    GammaValue1[i, 0] = GValue;
                    GammaValue1[i, 1] = GValue;
                    GammaValue1[i, 2] = GValue;

                    GammaValue2[i, 0] = GValue;
                    GammaValue2[i, 1] = GValue;
                    GammaValue2[i, 2] = GValue;

                    GammaValue3[i, 0] = GValue;
                    GammaValue3[i, 1] = GValue;
                    GammaValue3[i, 2] = GValue;

                    GammaValue4[i, 0] = GValue;
                    GammaValue4[i, 1] = GValue;
                    GammaValue4[i, 2] = GValue;

                    GammaValue5[i, 0] = GValue;
                    GammaValue5[i, 1] = GValue;
                    GammaValue5[i, 2] = GValue;

                    GammaValue6[i, 0] = GValue;
                    GammaValue6[i, 1] = GValue;
                    GammaValue6[i, 2] = GValue;

                    GammaValue7[i, 0] = GValue;
                    GammaValue7[i, 1] = GValue;
                    GammaValue7[i, 2] = GValue;

                    GammaValue8[i, 0] = GValue;
                    GammaValue8[i, 1] = GValue;
                    GammaValue8[i, 2] = GValue;

                    GammaValue9[i, 0] = GValue;
                    GammaValue9[i, 1] = GValue;
                    GammaValue9[i, 2] = GValue;
                }
                else
                {
                    GValue = (uint)(GammaValueMax * Point_Hi3288[i] / 255);
                    GammaValue[i, 0] = GValue;
                    GammaValue[i, 1] = GValue;
                    GammaValue[i, 2] = GValue;

                    GammaValue0[i, 0] = GValue;
                    GammaValue0[i, 1] = GValue;
                    GammaValue0[i, 2] = GValue;

                    GammaValue1[i, 0] = GValue;
                    GammaValue1[i, 1] = GValue;
                    GammaValue1[i, 2] = GValue;

                    GammaValue2[i, 0] = GValue;
                    GammaValue2[i, 1] = GValue;
                    GammaValue2[i, 2] = GValue;

                    GammaValue3[i, 0] = GValue;
                    GammaValue3[i, 1] = GValue;
                    GammaValue3[i, 2] = GValue;

                    GammaValue4[i, 0] = GValue;
                    GammaValue4[i, 1] = GValue;
                    GammaValue4[i, 2] = GValue;

                    GammaValue5[i, 0] = GValue;
                    GammaValue5[i, 1] = GValue;
                    GammaValue5[i, 2] = GValue;

                    GammaValue6[i, 0] = GValue;
                    GammaValue6[i, 1] = GValue;
                    GammaValue6[i, 2] = GValue;

                    GammaValue7[i, 0] = GValue;
                    GammaValue7[i, 1] = GValue;
                    GammaValue7[i, 2] = GValue;

                    GammaValue8[i, 0] = GValue;
                    GammaValue8[i, 1] = GValue;
                    GammaValue8[i, 2] = GValue;

                    GammaValue9[i, 0] = GValue;
                    GammaValue9[i, 1] = GValue;
                    GammaValue9[i, 2] = GValue;

                }
            }
        }
        private void num51Value_ValueChanged(object sender, EventArgs e)
        {
            txttip.Clear();
            int tep51Value = (int)num51Value.Value * 4;
            tep51Value += 0x8000;  //MSB为51 使能位
            showmsg("51实际设定值为*4+0x8000：" + tep51Value.ToString("X4"));
            string str;
            str = "51," + tep51Value.ToString("x4").Substring(2, 2) + "," + tep51Value.ToString("x4").Substring(0, 2);
            regSend.Text = str;
        }
        private void btnHisiRead0_Click(object sender, EventArgs e)
        {
            string str;
            str = txtHisiReg.Text;
            txtHisiReg.Text = txtHisiReg0.Text;
            btnHisiRead_Click(btnHisiRead, null);
            txtHisiReg.Text = str;
        }
        private void btnHisiRead_Click(object sender, EventArgs e)
        {
            string str;
            string[] Regpar = new string[5];
            uint[] HisiReg = new uint[2];
            str = txtHisiReg.Text;
            str = str.Replace(" ", ""); //去空格
            str = str.Replace(",", " "); // “;”换成空格
            Regpar = str.Trim().Split(' ');
            try
            {
                //byte.Parse((Convert.ToInt32(strTmp, 16)).ToString());  
                HisiReg[0] = (Convert.ToUInt32(Regpar[0], 16));
                if (Regpar.Length == 2)
                {
                    HisiReg[1] = (Convert.ToUInt32(Regpar[1])); //  Convert.float(GammadbvGroup[1]);
                }
                else HisiReg[1] = 1;
            }
            catch (FormatException ee)
            { Console.WriteLine(ee.Message); }
            uint[] HisiRegPar = new uint[HisiReg[1]];
            str = "";
            for (uint i = 0; i < HisiReg[1]; i++)
            {
                DDIC_Read_Register(HisiReg[0] + i * 4, 4);
                //showmsg(readValue.ToString("X8"));
                //HisiRegPar[i] = readValue;
                str += "0x" + (HisiReg[0] + i * 4).ToString("X8") + ",0x" + readValue.ToString("X8") + "\r\n";
            }
            txttip.Text = str;
            //showmsg("---FlahsID: " + DDIC_Read_FlashID().ToString("X8")      );
        }
        string filePath = "";
        private void btnOpenGammaFiles_Click(object sender, EventArgs e)
        {
            try
            {
                if (filePath == "")
                {
                    filePath = Application.StartupPath + @"\l_gamma";
                }
                System.Diagnostics.Process.Start(filePath);
            }
            catch { return; }
        }

        private void btnLoadMGroupGamma_Click(object sender, EventArgs e)
        {
            txt51Dbv_TextChanged(null, null);
            for (byte nn = 0; nn < TotalGammaNum; nn++)
            {
                gammaindex = nn;
                btnSaveGamma_Click(btnLoadGamma_Auto, null);
            }
            GPN = (byte)Point_Hi3288.Length; // 获取Gamma绑点数量
            GammavalMove(0, 0, GammaValx, GammaValue); //把第一组Gamma从GammaValx搬出至GammaValue
            if (chkGammaGridViewFresh.Checked)   // 刷新UI界面GammaValue值显示
            {
                this.GammaReg_dataGridView.Rows.Clear();
                for (byte i = 0; i < GPN; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }
            }
        }
        private void btnEditDBVsetting_Click(object sender, EventArgs e)
        {
            if (btnEditDBVsetting.Tag != null)
            {//编辑结束
                btnEditDBVsetting.Tag = null;
                btnGroupGammaTune.Enabled = true;
                txt51Dbv.Enabled = false;
                btnEditDBVsetting.Text = "DBV编辑";
                txt51Dbv_TextChanged(null, null);
            }
            else
            {
                btnEditDBVsetting.Tag = "editing";
                btnGroupGammaTune.Enabled = false;
                txt51Dbv.Enabled = true;
                btnEditDBVsetting.Text = "保存锁定";
            }
        }
        private void chkAODMode_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.chkAODMode, "勾选时会自动发送39命令进入AOD模式，取消勾选会自动下发38命令退出AOD模式");
        }
        public double ELVDDC { get; private set; }

        #endregion //-
        uint g_OTP_CommonBin_wordlen;  //该长度从bin文件获取
        private void btnOTP_Click(object sender, EventArgs e)
        {
            HI3288_OTP_BURN();
        }
        private void HI3288_OTP_BURN()
        {
            uint ret = 0;
            showmsg("Hi3288-DDIC-CommonBin-OTP-Burn Start");
            //setp 0:  检测AVDD,VGH是否上电，若未上电则报异常后终止写入
            DDIC_Write_Register(0X02001030, 0X89); //otp-direct-write ,使能电压监测              //0X02001030,0X00044089 
            DDIC_Read_Register(0X02001030, 4); DDIC_Read_Register(0X02001030, 4);
            DDIC_Write_Register(0X02001030, 0X00);
            if ((readValue & 0x44000) == 0x44000)
            {
                showmsg("上电异常，烧录停止"); return;
            }
            //step 1: 设定VI视频通路方式，并将Common-OTP.bin传递到DDIC-GRAM-0X0B000000地址上
            F_COMMON_OTP_BIN_LOAD_TO_RAM();
            showmsg("g_OTP_CommonBin_wordlen:" + g_OTP_CommonBin_wordlen.ToString("x2"));
            if (!chkOTPen.Checked) { showmsg("-------------Hi3288 数据搬运完成,直接退出-------------\r\n"); return; }
            //Step 2: OTP burn prepare. -- Very important.	
            DDIC_OTP_Burn_Prepare();
            if (firstOTPFlag)
            {
                //Step 3: OTP burn configuration area.
                F_OTP_Burn_ConfigArea();
                //step3:reload--固定配置 -- Very important.	
                DDIC_OTP_Reload();
            }
            ///*--------------------烧写公用OTP bin-------------------------------------------------------*/
            //step4: 将 Common-OTP.bin从DDIC-GRAM写入OTP内。 (0XFFFF07C0, 0X00000007:所有PQ_LUT都存FLASH)
            //--------------Common-OTP.bin字节长度=g_OTP_CommonBin_wordlen*4 ;
            //uint g_OTP_CommonBin_wordlen = 12536 >> 2; //Use 0XF00 to replace , no-used data will be ignore.
            ret = DDIC_OTP_Multigroupx_Write_Data(0xcfff0fc0, 0X00000387, 0X0B000000, g_OTP_CommonBin_wordlen);
            //！！！！上面前两个参数可能会随版本更新发生变化
            //            烧写OTP.bin时， g_OTP_CommonBin_wordlen为OTP.bin的字节数大小除以4。
            if (ret != 0)
            {
                MessageBox.Show(">>>>>Hi3288V100-DDIC-OTP-Burn Error<<<<<<\n");
                return;
            }
            DDIC_OTP_Reload();
            if (0 == F_OTP_CRC_FLAG())
            {
                showmsg("----烧写成功-----");
            }
            ///*--------------------公用OTP bin烧写完成-------------------------------------------------------------------------------------*/
            DDIC_OTP_Burn_removeVPPpower();
            showmsg("-------------Hi3288V100-DDIC-OTP-Burn  Completely-------------\r\n");
        }
        void DDIC_OTP_Burn_Prepare()
        {
            showmsg("---DDIC_OTP_Burn_Prepare---\n");
            //step3: AVDD上电，sleep out,vpp内供--固定配置
            DDIC_Write_Register(0x01004004, 0x0);      //进入 sleep-out,跳出休眠状态。
            DDIC_Write_Register(0x01004010, 0x0);      //0：Display OFF. OTP烧写在屏幕不亮的情况下执行。
            Thread.Sleep(10);
            DDIC_Write_Register(0x300002C, 0x25);        //VGH档位设定, VGH=AVDD+VCI
            Thread.Sleep(10);
            DDIC_Write_Register(0x30002bc, 0x1E1E);      //VGHR_normal(ldo2/ldo1)档位设定,normal：VGHR1=VGHR2=8V
            Thread.Sleep(10);
            DDIC_Write_Register(0x30000b8, 0x00004a08);  //Close LDO Bypass.
            Thread.Sleep(10);
            //Caution: Very important.非常重要！
            DDIC_Write_Register(0x030002BC, 0x240B);  //烧写OTP时，该VGHRH寄存器的配置值为： 0x30002BC为0x240B，测量VRGH=6V	
            //DDIC_Write_Register(0x03000000, 0x1);     //Use internal power-supply
            Thread.Sleep(10);
            if (chkOTPPowerON.Checked)
            {
                DDIC_Write_Register(0x03000000, 0x1);     //Use internal power-supply
            }
            else
            {
                DDIC_Write_Register(0x03000000, 0x0);     //Use internal power-supply
            }
        }
        void F_OTP_Burn_ConfigArea()
        {
            //Very important: 下面初始化参数由IC厂和屏厂共同确定！
            //Very important config for OTP. Hisi and Panel-vendor defined below setting.
            ///*------------------------------------------------------开始烧写OTP配置空间-------------------------------------------------------------------------------------*/	
            //step4: 烧写OTP配置空间--根据机型可变配置
            //Dump out OTP-Burn Message, Total Burn OTP-Group-num27: 
            //index:  0: GroupId: 5, GroupBurnTime: 2, GroupWordLen: 0x00000008(8) words, IsSaveOTPFlag: 0
            DDIC_OTP_groupx_Write_Len(5, 8);
            DDIC_OTP_groupx_Write_TimeNum(5, 2);
            //index:  1: GroupId: 6, GroupBurnTime: 4, GroupWordLen: 0x000000d0(208) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(6, 208);
            DDIC_OTP_groupx_Write_TimeNum(6, 4);
            //index:  2: GroupId: 7, GroupBurnTime: 4, GroupWordLen: 0x00000078(120) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(7, 120);
            DDIC_OTP_groupx_Write_TimeNum(7, 4);
            //index:  3: GroupId: 8, GroupBurnTime: 4, GroupWordLen: 0x00000072(114) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(8, 114);
            DDIC_OTP_groupx_Write_TimeNum(8, 4);
            //index:  4: GroupId: 9, GroupBurnTime: 4, GroupWordLen: 0x00000018(24) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(9, 24);
            DDIC_OTP_groupx_Write_TimeNum(9, 4);
            //index:  5: GroupId: 10, GroupBurnTime: 4, GroupWordLen: 0x000000a0(160) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(10, 160);
            DDIC_OTP_groupx_Write_TimeNum(10, 4);
            //index:  6: GroupId: 11, GroupBurnTime: 4, GroupWordLen: 0x00000280(640) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(11, 640);
            DDIC_OTP_groupx_Write_TimeNum(11, 4);
            //index:  7: GroupId: 16, GroupBurnTime: 4, GroupWordLen: 0x0000006e(110) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(16, 110);
            DDIC_OTP_groupx_Write_TimeNum(16, 4);
            //index:  8: GroupId: 17, GroupBurnTime: 4, GroupWordLen: 0x00000018(24) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(17, 24);
            DDIC_OTP_groupx_Write_TimeNum(17, 4);
            //index:  9: GroupId: 18, GroupBurnTime: 4, GroupWordLen: 0x0000004a(74) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(18, 74);
            DDIC_OTP_groupx_Write_TimeNum(18, 4);
            //index:  10: GroupId: 19, GroupBurnTime: 4, GroupWordLen: 0x0000004a(74) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(19, 74);
            DDIC_OTP_groupx_Write_TimeNum(19, 4);
            //index:  11: GroupId: 20, GroupBurnTime: 4, GroupWordLen: 0x00000038(56) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(20, 56);
            DDIC_OTP_groupx_Write_TimeNum(20, 4);
            //index:  12: GroupId: 21, GroupBurnTime: 4, GroupWordLen: 0x00000018(24) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(21, 24);
            DDIC_OTP_groupx_Write_TimeNum(21, 4);
            //index:  13: GroupId: 22, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(22, 40);
            DDIC_OTP_groupx_Write_TimeNum(22, 4);
            //index:  14: GroupId: 23, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(23, 40);
            DDIC_OTP_groupx_Write_TimeNum(23, 4);
            //index:  15: GroupId: 24, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(24, 40);
            DDIC_OTP_groupx_Write_TimeNum(24, 4);
            //index:  16: GroupId: 25, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(25, 40);
            DDIC_OTP_groupx_Write_TimeNum(25, 4);
            //index:  17: GroupId: 26, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(26, 40);
            DDIC_OTP_groupx_Write_TimeNum(26, 4);
            //index:  18: GroupId: 27, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(27, 40);
            DDIC_OTP_groupx_Write_TimeNum(27, 4);
            //index:  19: GroupId: 30, GroupBurnTime: 4, GroupWordLen: 0x00000012(18) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(30, 18);
            DDIC_OTP_groupx_Write_TimeNum(30, 4);
            //index:  20: GroupId: 31, GroupBurnTime: 4, GroupWordLen: 0x00000012(18) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(31, 18);
            DDIC_OTP_groupx_Write_TimeNum(31, 4);
            //index:  21: GroupId: 32, GroupBurnTime: 4, GroupWordLen: 0x00000012(18) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(32, 18);
            DDIC_OTP_groupx_Write_TimeNum(32, 4);
            //index:  22: GroupId: 33, GroupBurnTime: 4, GroupWordLen: 0x0000000e(14) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(33, 14);
            DDIC_OTP_groupx_Write_TimeNum(33, 4);
            //index:  23: GroupId: 34, GroupBurnTime: 4, GroupWordLen: 0x00000012(18) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(34, 18);
            DDIC_OTP_groupx_Write_TimeNum(34, 4);
            //index:  24: GroupId: 39, GroupBurnTime: 4, GroupWordLen: 0x00000080(128) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(39, 128);
            DDIC_OTP_groupx_Write_TimeNum(39, 4);
            //index:  25: GroupId: 40, GroupBurnTime: 4, GroupWordLen: 0x0000011a(282) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(40, 282);
            DDIC_OTP_groupx_Write_TimeNum(40, 4);
            //index:  26: GroupId: 41, GroupBurnTime: 4, GroupWordLen: 0x00000028(40) words, IsSaveOTPFlag: 1
            DDIC_OTP_groupx_Write_Len(41, 40);
            DDIC_OTP_groupx_Write_TimeNum(41, 4);

            Thread.Sleep(5);
            showmsg("F_OTP_Burn_ConfigArea Done\r\n");
        }
        void DDIC_OTP_groupx_Write_Len(uint Group_index, uint Groupx_wordlen)
        {
            uint value = 0;
            uint burnAddr = 0;

            showmsg("//Group%d length is %d words\n" + Group_index + "_" + Groupx_wordlen);
            DDIC_Write_Register(0x02000000, 0x00000019); //otp-direct-write
            burnAddr = (0x48 + ((Group_index >> 1) << 2)) << 3;
            DDIC_Write_Register(0x02000004, burnAddr);

            if (0 == (Group_index % 2))
            {
                value = Groupx_wordlen;
            }
            else
            {
                value = Groupx_wordlen << 16;
            }
            DDIC_Write_Register(0x02000010, value);     //Set value
            DDIC_Write_Register(0x02000014, 0x3c5aa5);  //OTP-Ctrl reg-up
                                                        //showmsg("OTP_BurnLen grip : %d burnAddr = 0x%x, value = 0x%x\n", Group_index, burnAddr, value);
            showmsg("Delayms10ms");
            Thread.Sleep(50);
        }

        void DDIC_OTP_groupx_Write_TimeNum(uint Group_index, uint time_num)
        {
            uint value = 0;
            uint index = 0;
            uint burnTimeAddr = 0;
            showmsg("//Burn times: Group%d Burn times is %d\n" + Group_index + "_" + time_num);

            burnTimeAddr = (0x30 + ((Group_index >> 3) << 2)) << 3;
            value = time_num;
            for (index = 1; index <= (Group_index % 8); index++)
            {
                value = value * 16;
            }
            DDIC_Write_Register(0x02000000, 0x00000019); //otp-direct-write
            DDIC_Write_Register(0x02000004, burnTimeAddr);
            DDIC_Write_Register(0x02000010, value);     //Set value
            DDIC_Write_Register(0x02000014, 0x3c5aa5);  //OTP-Ctrl reg-up
            showmsg("Delayms10");
            Thread.Sleep(50);
        }

        void DDIC_OTP_Burn_removeVPPpower()
        {
            showmsg("---DDIC_OTP_remove VPPpower---\n");
            DDIC_Write_Register(0x03000000, 0x0);  //Use internal power-supply
            Thread.Sleep(10);
            DDIC_Write_Register(0x030000bc, 0x00000f);
            Thread.Sleep(10);
            DDIC_Write_Register(0x030000bc, 0x00003f);
            Thread.Sleep(10);
            DDIC_Write_Register(0x030000B8, 0x4522);
            Thread.Sleep(10);
            DDIC_Write_Register(0x030002bc, 0x1e1e);
            Thread.Sleep(10);
            DDIC_Write_Register(0x030002b8, 0x1e1e);
            Thread.Sleep(10);
        }
        void DDIC_OTP_Reload()
        {
            showmsg("DDIC_OTP_Reload\n");
            DDIC_Write_Register(0X01000060, 0X00010303);
            Thread.Sleep(100);
            DDIC_Write_Register(0X01000060, 0X00000303);
            Thread.Sleep(100);
            DDIC_Write_Register(0x01004080, 0X02D00181);
            Thread.Sleep(100);
            DDIC_Write_Register(0x01004080, 0X02D20181);
            Thread.Sleep(100);
        }
        uint F_OTP_CRC_FLAG()
        {
            //检查OTP-CRC-Flag
            uint value_1C = DDIC_Read_Register(0x0200001c, 4);
            value_1C = DDIC_Read_Register(0x0200001c, 4);
            uint value_24 = DDIC_Read_Register(0x02000024, 4);
            uint value_28 = DDIC_Read_Register(0x02000028, 4);
            if ((0x11 == (value_1C & 0x3f)) && (0 == value_24) && (0 == value_28))
            {
                //CRC-Flag正确
                return 0;
            }
            else
            {
                //CRC-Flag异常！
                return 1;
            }
        }
        void F_COMMON_OTP_BIN_LOAD_TO_RAM()       // 2022.9
        {
            //trans data for VI road//RAM切换接收bin/显示数据
            showmsg("trans data for VI road\n");
            DDIC_Write_Register(0X02006034, 0X00221100);
            DDIC_Write_Register(0X05000000, 0X02030003);
            DDIC_Write_Register(0X0500000C, 0X00000000);
            DDIC_Write_Register(0X05000040, 0X00010000);
            DDIC_Write_Register(0X05000014, 0X00000000);
            DDIC_Write_Register(0X05000018, 0X00000060);
            DDIC_Write_Register(0X05001000, 0X00000000);
            DDIC_Write_Register(0X05002000, 0X00000000);
            DDIC_Write_Register(0X0600C044, 0X40000000);
            //RAM切换接收bin/显示数据
            exitUCSFF0000();    // PG_MIPI_WRITE(0, 0x29, 0xFF, 0x00, 0x00, 0x00);  //Reset page/bank/offset =0.	
            Thread.Sleep(100);
            //--------加载bin至ram，2c/3c下发------------------------------------------------------------------------
            //LOADBIN("G_OTP_ByeData.bin\n");
            //Infor: Please load the OTP.bin into PG buffer: G_OTP_ByeData[]
            //数据发送长度以96byte为最小单位，可以为96倍数
            Int32 File_Length;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded;

            string FileName = Application.StartupPath + @"\x_Hisi3288\OTP_test_otp_H6.bin";//txtDemuraAddr.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
            if (chkCommonBinAddr.Checked)
            {
                FileName = txtDemuraAddr.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
            }
            FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            File_Length = Convert.ToInt32(aFile.Length);
            g_OTP_CommonBin_wordlen = (uint)File_Length >> 2;
            DemuraBinSize = File_Length;


            if (DemuraBinSize % 96 != 0)
            {
                showmsg("Bin文件不能被96整除，已经选择自动补齐后的新文件！");
                byte[] arr96n = new byte[(DemuraBinSize / 96 + 1) * 96];
                //g_OTP_CommonBin_wordlen =(uint) arr96n.Length >> 2;
                FileName += "Resiz96.bin";
                System.IO.FileStream fs = File.OpenWrite(FileName);   //  //输出文件
                aFile.Read(arr96n, 0, File_Length);
                if (arr96n[arr96n.Length - 1] == 0)
                { arr96n[arr96n.Length - 1] = 6; }
                if (arr96n[arr96n.Length - 2] == 0)
                { arr96n[arr96n.Length - 2] = 9; }
                fs.Write(arr96n, 0, arr96n.Length);
                fs.Close(); fs.Dispose();
                showmsg("--新bin文件保存OK!--");
            }


            showmsg("BinSize:" + DemuraBinSize + "Byte");
            showmsg("BinFile new Size:" + ((DemuraBinSize / 96 + 1) * 96) + "Byte");
            showmsg("-------------------");

            aFile.Seek(0 + 0x2584, SeekOrigin.Begin);    //跳转
            ByteReaded = aFile.Read(TemFileContent, 0, 2); //
            showmsg("CRC:" + String.Format("{0:X2}", TemFileContent[0]) + "," + String.Format("{0:X2}", TemFileContent[1]) + "\r\n");
            showmsg("The transfer is complete" + "\r\n");
            aFile.Close(); Thread.Sleep(100);

            if (frmTop.DemuraBinLoad("00", "" + nummipiPPP.Value, "2C", "3C", FileName) == 1) //packtype 00=下位机不加地址
            //   (string packtype, string packsize, string Addr_first, string Addr_second, string BinFilePath)//packsize 必须是偶数
            {
                showmsg("Bin Load OK");
            }
            else
            {
                showmsg("Bin Load NG");
                return;
            }
            //----------------------------------------------------------------------------
            Thread.Sleep(100);
        }
        uint DDIC_OTP_Multigroupx_Write_Data(uint Group_num1, uint Group_num2, uint GRAMDataAddress, uint Data_wordlen)
        {
            if (Data_wordlen > 0XF00)
            { //DDIC_OTP_Multigroupx_Write_Data
                showmsg("Fatail Error: DDIC_OTP_Burn len [%d] > 0XF00  is too big, Exit!!\n" + Data_wordlen);
                return 1;
            }
            //数据搬入0x0b000000之后，执行下方代码进行烧录
            uint i = 0;
            uint value = 0;
            //烧写otp-mulit-group的数据空间配置:
            DDIC_Write_Register(0X02000000, 0X00000009);

            //配置烧写Mulit-group_index(0-47可按bit组合).
            DDIC_Write_Register(0X02000008, Group_num1);
            DDIC_Write_Register(0X0200000C, Group_num2);

            DDIC_Write_Register(0X02000014, 0X003C5AA5);
            //DMAC seting gram2otp
            //CRG
            DDIC_Write_Register(0x0100003C, 0X00000001);
            DDIC_Write_Register(0x01000058, 0x10001);
            DDIC_Write_Register(0x01000058, 0X00000001);
            //DMA
            DDIC_Write_Register(0x01007030, 0X00000001);
            DDIC_Write_Register(0x01007008, 0X000000FF);
            DDIC_Write_Register(0x01007010, 0X000000FF);

            DDIC_Write_Register(0x01007100, GRAMDataAddress);  //DMAC_CX_SRC_ADDR: 源地址寄存器，给出当前待传数据的源地址（字节排序）。寄存器的偏移地址为0x100+X*0x20。OTP-Groupx-GRAM-Address.Default:0X0B000000.
            DDIC_Write_Register(0x01007104, 0X02000010);       //DMAC_CX_DEST_ADDR:目的地址寄存器，偏移地址为：0x104+X*0x20。

            showmsg("OTP-DMA transfer %d words, Start ...\n" + Data_wordlen);
            if (Data_wordlen > 0XF00)
            { //DDIC_OTP_Multigroupx_Write_Data
                showmsg("Fatail Error: DDIC_OTP_Burn len [%d] > 0XF00  is too big, Exit!!\n" + Data_wordlen);
                return 1;
            }
            value = 0x8449B000 + Data_wordlen;            //Data_wordlen unit is word(32bit), defualt-value:0XF00. 未采用链表。
            DDIC_Write_Register(0x0100710C, value);       //DMAC_CX_CONTROL: 
            DDIC_Write_Register(0x01007110, 0x0000c001);  //DMAC_CX_CONFIG: 

            Thread.Sleep(1000);  //1000 ms very important.
                                 //重复读0x1007014的值，bit0=1表示公共的数据空间烧写完成
            for (i = 0; i <= 10; i++)
            {
                uint dmavalue = DDIC_Read_Register(0x1007014, 4);
                uint value_1C = DDIC_Read_Register(0x0200001c, 4);

                if (((dmavalue & 0x01) == 1) && (0x01 == (value_1C & 0x03)))
                {
                    showmsg("Write common OTP done\n");
                    showmsg("****DDIC_OTP_Multigroupx_Write_Data OUT****\n");
                    return 0;
                }
                Thread.Sleep(500);  //mMSG.PRINTLN
            }
            showmsg("DDIC_OTP_Multigroupx_Write_Data FAIL---:-<<<<");
            return 1;
        }
        uint F_OTP_GROUP_GET_MAX_BURN_TIMES(uint group_num)
        {
            //if ((group_num > OtpGroupBurnEnd) || (group_num < OtpGroupBurnBegin))
            //{
            //    showmsg("Error group num %d, please input %d ~ %d\n", group_num, OtpGroupBurnBegin, OtpGroupBurnEnd);
            //}

            byte idx = (byte)(group_num % 8);
            uint reg_val = 0;
            uint times = 0;
            uint offset = (group_num / 8) * 4;
            DDIC_Read_Register(0x02000070 + offset, 4);

            reg_val = readValue;
            times = (reg_val >> (idx * 4)) & 0xF;
            //showmsg("Group %d is allowed to be burned up to %d times\n", group_num, times)
            return times;
        }

        private void btnGammaBinCreat_Click(object sender, EventArgs e)
        {
            txtGammaNode_TextChanged(null, null);
            GetGammabin();
            showmsg("---GammaBin已更新---");
        }

        private void btnOpenBinFolder_Click(object sender, EventArgs e)
        {
            try
            {
                if (filePath == "")
                {
                    filePath = Application.StartupPath + @"\x_Hisi3288";
                }
                System.Diagnostics.Process.Start(filePath);
            }
            catch { filePath = Application.StartupPath + @"\x_Hisi3288"; return; }
        }
        private void btnReadGroup_Click(object sender, EventArgs e)
        {
            //# ddic_write
            //            // DMA把某grp数据搬到DDIC-GRAM 0x0b000000位置
            //            0x1000010   0x3
            //0x100003c   0x1
            //0x1000058   0x1
            ////OTP-读操作，间接访址模式
            //0x2000000   0x1
            ////group0~31配置   (按bit设置)
            //0x2000008   0x40
            ////group32~48配置，  (按bit设置)
            //0x200000C   0x00
            //0x2000014   0x3c5aa5
            //0x1007030   0x1
            //0x1007100   0x02000018
            //0x1007104   0x0b000000
            //0x100710c   0x88490900    //0x*****900  900个word
            //0x1007110   0x8001
            //#
            //#ddic_read
            //0x0b000000 32
            //#
            rdoHisiCode.Checked = true;
            string str =
                   "0x1000010,0x3" + "\r\n"
                + "0x100003c,0x1" + "\r\n"
                + "0x1000058,0x1" + "\r\n"
                + "0x2000000,0x1" + "\r\n" ////OTP-读操作，间接访址模式
                + "0x2000008,0x" + (1 << ((byte)(numGroupSelect0_31.Value))).ToString("x2") + "\r\n"//group0~31配置   (按bit设置)
                + "0x200000C,0x" + (1 << ((byte)(numGroupSelect32_48.Value - 32))).ToString("x2") + "\r\n"  //group32~48配置，  (按bit设置)
                + "0x2000014,0x3c5aa5" + "\r\n"
                + "0x1007030,0x1" + "\r\n"
                + "0x1007100,0x02000018" + "\r\n"
                + "0x1007104,0x0b000000" + "\r\n"
                + "0x100710c,0x88490900" + "\r\n" //0x*****900  900个word
                + "0x1007110,0x8001" + "\r\n";
            if (rdoHisiCode.Checked)
            {
                txt3288Input.Text = str;
                btnCodeConvert_Click(null, null);
                btnCmdSendX_Click(null, null);
                Thread.Sleep(200);
                Application.DoEvents();//重点，必须加上，否则父子窗体都假死      
            }
            //txtCmd3.Text = "";
            txtHisiReg.Text = "0x0b000000," + numRedaBack.Value;
            btnHisiRead_Click(btnHisiRead, null);
            numRedaBack.Enabled = true;
        }

        //byte[] g_ReadData=new byte [256];
        uint F_OTP_GROUP_GET_BURNED_TIMES(uint group_num)
        {//Group 总数？
            byte i = 0;
            //if ((group_num > OtpGroupBurnEnd) || (group_num < OtpGroupBurnBegin))
            //{
            //    showmsg("Error group num %d, please input %d ~ %d\n-"+ group_num+"_"+OtpGroupBurnBegin + "_" + OtpGroupBurnEnd);
            //}
            uint count = 0;
            byte idx = (byte)(group_num % 4);
            uint offset = (group_num / 4) * 4;
            DDIC_Read_Register(0x02000040 + offset, 4);
            for (i = 0; i <= 7; i++)
            {
                if (((((readValue >> (idx * 8)) & 0xff) >> i) & 0x1) == 1)  //(((g_ReadData[idx] >> i) & 0x1)==1)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            //showmsg("Group %d  has been burned (g_ReadData 0x%x) %d times\n" + "_" + group_num + "_" + g_ReadData[idx] + "_" + count);
            return count;
        }
        bool firstOTPFlag = true;
        private void btnHisiCodeTest_Click(object sender, EventArgs e)
        {
            uint x, y;
            if (0 == F_OTP_CRC_FLAG())
            {
                showmsg("----烧写成功-----");
            }
            else
            {
                showmsg("----当前屏commonbin烧录 CRC 校验未通过-----");
            }
            x = F_OTP_GROUP_GET_BURNED_TIMES(6); x = F_OTP_GROUP_GET_BURNED_TIMES(6);
            y = F_OTP_GROUP_GET_MAX_BURN_TIMES(6);
            y = F_OTP_GROUP_GET_MAX_BURN_TIMES(6);
            if (y != 0) { firstOTPFlag = false; }
            else { firstOTPFlag = true; }

            showmsg("Group6 burned times:" + x);
            showmsg("Group6 MAX_BURN_TIMES:" + y);
            x = 5;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x)); Delay(100);
            x = 7;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 8;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 7;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 9;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 10;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 11;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 16;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 17;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 18;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 19;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 20;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 21;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 22;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 23;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 24;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 25;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 26;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 27;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 30;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 31;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 32;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 33;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 34;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 39;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 40;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));
            x = 41;
            showmsg("Group" + x + " burned times:" + F_OTP_GROUP_GET_BURNED_TIMES(x));

            //读0xc000000的低24bit就是flash的id

            //uint xwxx00 = DDIC_Read_Register(0x0b352760, 4);
            //uint wxxx10 = DDIC_Read_Register(0x0b352740, 4);
            //uint wxxx20 = DDIC_Read_Register(0x0b326f8c, 4);
            //DDIC_Write_Register(0X0B089FFC, 0XF0AD10EE);
            //uint wxxx30 = DDIC_Read_Register(0X0B089FFC, 4);
            //uint wxxx31 = DDIC_Read_Register(0X0B089FFC, 4);
            //Demuratest();
            //uint xxx = DDIC_Read_Register(0x0b000790, 4);
            ////uint FlashID = DDIC_Read_FlashID();
            //uint xxx0 = DDIC_Read_Register(0x0b000780, 4);
            //uint xxx1 = DDIC_Read_Register(0x0b000784, 4);
            //uint xxx2 = DDIC_Read_Register(0x0b000788, 4);
            //uint xxx3 = DDIC_Read_Register(0x0b00078c, 4);
            //uint xxx5 = DDIC_Read_Register(0x0b000790, 4);
            //uint xxx6 = DDIC_Read_Register(0x0b0007a4, 4);
            //uint xxx7 = DDIC_Read_Register(0x0b0007b8, 4);
            //uint xxx8 = DDIC_Read_Register(0x0b0007cc, 4);
            //uint xxx9 = DDIC_Read_Register(0x0b0007d0, 4);
            //uint xxx10 = DDIC_Read_Register(0x0b0007e4, 4);
            //uint xxx11 = DDIC_Read_Register(0x0b089fe0, 4);
            //uint xxx12 = DDIC_Read_Register(0x0b089ffc, 4);
            //Console.WriteLine("---------------------------");
            //Console.WriteLine("xxx0:0x{0:X8}", xxx0);
            //Console.WriteLine("xxx1:0x{0:X8}", xxx1);
            //Console.WriteLine("xxx2:0x{0:X8}", xxx2);
            //Console.WriteLine("xxx3:0x{0:X8}", xxx3);

            //Console.WriteLine("xxx5:0x{0:X8}", xxx5);
            //Console.WriteLine("xxx6:0x{0:X8}", xxx6);
            //Console.WriteLine("xxx7:0x{0:X8}", xxx7);
            //Console.WriteLine("xxx8:0x{0:X8}", xxx8);
            //Console.WriteLine("xxx9:0x{0:X8}", xxx9);
            //Console.WriteLine("xxx10:0x{0:X8}", xxx10);
            //Console.WriteLine("xxx11:0x{0:X8}", xxx11);
            //Console.WriteLine("xxx12:0x{0:X8}", xxx12);
            //uint xxx13 = DDIC_Read_Register(0x050000c4, 4);
            //Console.WriteLine("xxx13:0x{0:X8}", xxx13);
            //DDIC_Product_BurnFlashBin(565248,0);
            //Console.WriteLine("FlashIDis:0x{0:X8}", FlashID);
        }
    }
}
